db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Db-derby Wiki] Update of "IntroToJUnit" by SusanCline
Date Tue, 29 Aug 2006 23:25:31 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification.

The following page has been changed by SusanCline:
http://wiki.apache.org/db-derby/IntroToJUnit

------------------------------------------------------------------------------
  
  == Topics ==
  
-  1 Introduction to JUnit
+  1. Introduction to JUnit
-  2 Where to get JUnit
+  2. Where to get JUnit
-  3 Running the Examples
+  3. Running the Examples
-  4 JUnit API
+  4. JUnit API
-  5 Creating your First Test (Command Line and Eclipse)
+  5. Creating your First Test (Command Line and Eclipse)
-  6 Fixtures
+  6. Fixtures
-  7 Suites
+  7. Suites
-  8 More about Assertions
+  8. More about Assertions
-  9 JUnit tests and the Derby test harness
+  9. JUnit tests and the Derby test harness
-  10 Resources
+  10. Resources
  
  
  == Introduction to JUnit ==
  
- Writing tests during development
+ Write tests during development [[BR]] 
- Code a little, test a little, ad infinitum
+ Code a little, test a little, ad infinitum [[BR]] 
- Provides the structure of a test case and tools 
+ Provides the structure of a test case and tools [[BR]] 
  
-        Test Case - junit.framework.TestCase
+ Test Case: junit.framework.!TestCase
  
- 	Tools - junit.textui.  \
-                 junit.awtui.  --  TestRunner 
-                 junit.swingui. / 
- Test case
+ Tools: junit.textui.!TestRunner, junit.awtui.!TestRunner, junit.swingui.!TestRunner
+ 
+ When writing test cases:
+ 
- 	Create the objects
+  1. Create the objects 
- 	Exercise the objects
+  2. Exercise the objects	
- 	Verify the results
+  3. Verify the results
  
+ For more info on the background of JUnit, see JUnit Test Infected: Programmers Love Writing
Tests in the installed documentation: junit3.8.2/doc/testinfected/testing.htm.
- More info: JUnit Test Infected: Programmers Love Writing Tests
- In the installed documentation: junit3.8.2/doc/testinfected/testing.htm
  
  == Where to get JUnit ==
  
  http://prdownloads.sourceforge.net/junit/
- The Derby tests have been run against 3.8.1 
- Version 3.8.2 has been tested and the message reporting gives more context
+ 
+ The Derby tests have been run against 3.8.1 and 3.8.2, however 3.8.2 gives more
+ context in the reporting of failures and errors.
+ 
- Download junit3.8.2.zip
+  1. Download junit3.8.2.zip
- Unzip it
+  2. Unzip it
- Includes documentation and javadoc (API)
+  3. Includes documentation and javadoc (API)
  
  Example of 3.8.2 improvement:
  expected:<...st of the emergency [broadcasting] system> but was:<...st of the emergency
[locating] system> 
@@ -66, +67 @@

  OK (131 tests)
  }}}
  
- == JUnit API == 
+ '''JUnit API'''
  
- junit.framework.Test Interface has two methods
+ junit.framework.Test Interface has two methods 
- 	countTestCases() - counts the number of test cases this test (java class) will run
+  * countTestCases() - Counts the number of test cases this test (java class) will run
- 	run(TestResult) - Runs a test and collects its result in a TestResult instance
+  * run(!TestResult) - Runs a test and collects its result in a !TestResult instance
  
  junit.framework.Assert - contains multiple static assert methods
- 	assertEquals() methods with different parameters (overloaded method)
+  * assertEquals() methods with different parameters (overloaded method)
-        assertFalse()
+  * assertFalse()
- 	assertNotNull()
+  * assertNotNull()
- 	assertNotSame()
+  * assertNotSame()
- 	assertNull()
+  * assertNull()
- 	assertSame()
+  * assertSame()
- 	assertTrue(boolean)
+  * assertTrue(boolean)
- 	fail(String message)
+  * fail(String message)
  
- junit.framework.TestCase implements Test and extends Assert - contains convenience methods:
+ junit.framework.!TestCase implements Test and extends Assert - contains convenience methods:
- 	runTest() - the test(s) to run
+  * runTest() - the test(s) to run
- 	setUp() - initialize test variables, open network connections, etc.
+  * setUp() - initialize test variables, open network connections, etc.
- 	tearDown() - closes resources
+  * tearDown() - closes resources
          
- Your class must extend junit.framework.TestCase
+ Your class must extend junit.framework.!TestCase
  	
- 
  == Creating your First Test (Command Line and Eclipse) ==
  
-  1 Implement a subclass of TestCase (extend TestCase)
+  1. Implement a subclass of !TestCase (extend !TestCase)
-  2 Override the method runTest(), calling the tests to run
+  2. Override the method runTest(), calling the tests to run
-  3 Create a method to test, using one of the assertXXX methods to 
+  3. Create a method to test, using one of the assertXXX methods to compare test values
- compare test values
+ 
- 	The method to test must start with the prefix 'test', e.g. 'testAddMethod'
+ The method to test must start with the prefix 'test', e.g. 'testAddMethod'
  
  Example of the simplest test case, containing only one test:
  {{{
@@ -121, +121 @@

  }
  
  }}}
+ 
  ==== To test this class via the Command Line ====
  
-  1 make sure junit.jar is in your class path and the directory where the
+  1. make sure junit.jar is in your class path and the directory where the compiled classes
reside
- 	compiled classes reside
-  2 compile the test
+  2. compile the test
-  3 run the test using the tool junit.textui.TestRunner
+  3. run the test using the tool junit.textui.!TestRunner
  
- 
+ {{{
-  1 C:\junit\junit3.8.2>set CLASSPATH=C:\junit\junit3.8.2\junit.jar;classes
+ C:\junit\junit3.8.2>set CLASSPATH=C:\junit\junit3.8.2\junit.jar;classes
  
-  2 C:\junit\junit3.8.2> javac -d classes src/junitdemo/FirstJUnitTest.java 
+ C:\junit\junit3.8.2> javac -d classes src/junitdemo/FirstJUnitTest.java 
     (places the compiled class in the classes directory)
  
-  3 C:\junit\junit3.8.2>java junit.textui.TestRunner junitdemo.FirstJUnitTest
+ C:\junit\junit3.8.2>java junit.textui.TestRunner junitdemo.FirstJUnitTest
  .
  Time: 0
  
  OK (1 test) 
- 
+ }}}
  
  ==== To create and test this class via Eclipse ====
  
+  1. While in the Java Perspective, right click in the Package Explorer View, New >>
Project >> Java Project, Provide a name >> Click Next. In the Java Settings screen
select the Libraries tab, then click the Add External JARs button. 
-  1 While in the Java Perspective, right click in the Package Explorer View, New >>
Project >> Java Project, 
- Provide a name >> Click Next. In the Java Settings screen select the Libraries
- tab, then click the Add External JARs button. 
  
-  2 Browse to junit.jar, then click Finish.
+  2. Browse to junit.jar, then click Finish.
  
+  3. Right click the Project, New >> JUnit Test Case, package = junitedemo, class name=FirstJUnitTest,
Superclass = junit.framework.!TestCase, don't select any of the method stubs be created, leave
the Class under test blank, click 'Finish'.
-  3 Right click the Project, New >> JUnit Test Case, package = junitedemo, class name=FirstJUnitTest,

- Superclass = junit.framework.TestCase, don't select any of the method stubs be created,
leave the Class under test blank, click 'Finish'.
  	
-  4 Add the two methods shown above, runTest() and testAdd(), and save the file. 
+  4. Add the two methods shown above, runTest() and testAdd(), and save the file. 
  
+  5. To run the test right click FirstJUnitTest.java, Run As >> JUnit Test OR click
the icon on the Menu Bar with the green circle and white arrow, Run As >> JUnit Test.
-  5 To run the test right click FirstJUnitTest.java, Run As >> JUnit Test OR
- 	click the icon on the Menu Bar with the green circle and white arrow,
- 	Run As >> JUnit Test.
  
-  6 A new view in Eclipse will appear as a JUnit tab and show if there were any
+  6. A new view in Eclipse will appear as a JUnit tab and show if there were any Errors or
Failures. 
- 	Errors or Failures. 
  
  
  == Fixtures ==
@@ -173, +167 @@

  For a single test (method) contained in a single Test class these were the
  steps we needed to do:
  
-  1 Create a subclass of TestCase (extend TestCase)
+  1. Create a subclass of !TestCase (extend !TestCase)
-  2 Override the method runTest(), calling the tests to run
+  2. Override the method runTest(), calling the tests to run
-  3 Create a method to test, using one of the assertXXX methods to 
+  3. Create a method to test, using one of the assertXXX methods to 
  
  For a fixture use these steps:
  
-  1 Create a subclass of TestCase (extend TestCase)
+  1. Create a subclass of !TestCase (extend !TestCase)
-  2 Add an instance variable for each part of the fixture
+  2. Add an instance variable for each part of the fixture
-  3 Override setUp() to initialize the variables
+  3. Override setUp() to initialize the variables
-  4 Override tearDown() to release any permanent resources you allocated in setUp()
+  4. Override tearDown() to release any permanent resources you allocated in setUp()
  
  Below is an example of a Fixture class which contains five tests to run.
  (In reality you could have collapsed these tests into a fewer number.)
  
+ If you are using Eclipse to create this class, go ahead and select the main() and setUp()
method stubs be created this time. For the main() method, select 'Add !TestRunner statement
for: text ui'.
- If you are using Eclipse to create this class, go ahead and select the main() and setUp()

- method stubs be created this time. For the main() method, select 'Add TestRunner statement
for: text ui'.
  
  For this example we'll show how to run a JUnit test case with a main() in it
- instead of having to use the tool, junit.textui.TestRunner class, for our main
+ instead of having to use the tool, junit.textui.!TestRunner class, for our main
  class. 
  
  ==== JUnit Fixture class with a Main method ====
@@ -258, +251 @@

  
  Set the class path: 
  
- C:\junit\junit3.8.2>set CLASSPATH=C:\junit\junit3.8.2\junit.jar;classes
+  1. C:\junit\junit3.8.2>set CLASSPATH=C:\junit\junit3.8.2\junit.jar;classes
  
  Compile the class:
  
- C:\junit\junit3.8.2>javac -d classes src/junitdemo/FixtureJUnitTest.java
+  1.#2 C:\junit\junit3.8.2>javac -d classes src/junitdemo/FixtureJUnitTest.java
  
  Run the class (since there is a main method, just run it normally):
  {{{
@@ -272, +265 @@

  There was 1 failure:
  1) testStringSameObject2(junitdemo.FixtureJUnitTest)junit.framework.AssertionFai
  ledError: 
-         Fail: string1 is not the same as s2 expected same:<string1> was not:<s
+         Fail: string1 is not the same as s2 expected same:<string1> was not:<string1>
- tring1>
-         at junitdemo.FixtureJUnitTest.testStringSameObject2(FixtureJUnitTest.jav
+         at junitdemo.FixtureJUnitTest.testStringSameObject2(FixtureJUnitTest.java:52)
- a:52)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
  java:39)
@@ -297, +288 @@

  the case if you will be integrating them into the derby harness) just do the
  following;
  
-  1. remove the main method from FixtureJUnitTest
+  1. remove the main method from Fixture!JUnitTest
-  2. run the class with the junit.textui.TestRunner class like before
+  2. run the class with the junit.textui.!TestRunner class like before
- 
+ {{{
  C:\junit\junit3.8.2>java junit.textui.TestRunner junitdemo.FixtureJUnitTest
- 
+ }}}
  == Suites ==
  
- A TestSuite is the smallest execution unit in JUnit.   
+ A !TestSuite is the smallest execution unit in JUnit.   
  
  To run all of the tests in a class do the following:
  
   1. Create a one arg constructor which takes a String - the name of the method to run
-  2. create a suite() method which returns an implementation of the Test Interface,
+  2. create a suite() method which returns an implementation of the Test Interface, e.g.
!TestSuite
- 	e.g. TestSuite
  
  The class below, SuiteJUnitTest.java demonstrates this.
  
@@ -381, +371 @@

  
  }
  }}}
+ 
  If you want to specifically name which methods to run in the suite (for
  instance if you don't want to run all of them) change the above suite() method
  to this:
+ 
  {{{
  	// only adds two tests to the suite
  	public static Test suite() {
@@ -393, +385 @@

  		return suite;
  	}
  }}}
- The output from running the modified SuiteJUnitTest class, which only runs
+ The output from running the modified !SuiteJUnitTest class, which only runs
  two tests is shown below; 
  {{{
  C:\junit\junit3.8.2>java junit.textui.TestRunner junitdemo.SuiteJUnitTest
@@ -440, +432 @@

  
  }}}
  
- ===== SuiteTest1JUnitTest ====
- 
+ '''SuiteTest1JUnitTest'''
+ {{{
  package junitdemo;
  
  import junit.framework.Test;
@@ -499, +491 @@

  
  }}}
  
- ===== SuiteTest2JUnitTest =====
+ '''SuiteTest2JUnitTest'''
  
  {{{
  package junitdemo;
@@ -553, +545 @@

  Below are some code snippets that show examples of using the various
  assert methods.
  
- assertEquals
+ '''assertEquals'''
- ------------
  
  Overloaded for almost all primitive data types and the Object arguments.
  {{{
@@ -563, +554 @@

  assertEquals(expected, rValue, 0.0);
  }}}
  
- assertFalse
+ '''assertFalse'''
- -----------
  
  Takes a boolean argument.  Could use in place of assertTrue(boolean)
  {{{
@@ -572, +562 @@

  assertFalse(result == 10);
  }}}
  
- assertNotNull
+ '''assertNotNull'''
- -------------
  
  Takes an object with an optional second argument, the String message.
  {{{
@@ -581, +570 @@

  assertNotNull("Fail: Object s2 is null. ", s2);
  }}}
  
- assertNotSame
+ '''assertNotSame'''
- -------------
  
  Takes two objects with an optional third argument, the String message.
  {{{
@@ -590, +578 @@

  assertNotSame("Fail: string1 is the same as s2", "string1", s2)
  }}}
  
- assertNull
+ '''assertNull'''
- ----------
  
  Takes an object with an optional second argument, the String message.
  
@@ -600, +587 @@

  assertNull("Fail:object was not null", nullObj);
  }}}
  
- assertTrue
+ '''assertTrue'''
- ----------
+ 
  {{{
  int val1 = 5;
  int val2 = 6;
@@ -609, +596 @@

  assertTrue(result == 1);
  }}}
  
+ '''fail'''
- fail
- ----
  
  Fail takes one argument, the String message output and is used to fail a test.
  
@@ -637, +623 @@

  
  However, as of August 29th, 2006 this information is valid.
  
+  1. Create a JUnit test that extends the Derby class !BaseJDBCTestCase(org.apache.derbyTesting.junit.!BaseJDBCTestCase)
and put it in the correct package name space of the Derby test harness.   For instance, org.apache.!derbyTesting.!functionTests.tests.lang
  
- a) Create a JUnit test that extends the Derby class BaseJDBCTestCase
- (org.apache.derbyTesting.junit.BaseJDBCTestCase) and put it in the correct 
- package name space of the Derby test harness.  
- For instance, org.apache.derbyTesting.functionTests.tests.lang
- 
- b) Add a public constructor to your test class that takes a String argument 
+  2. Add a public constructor to your test class that takes a String argument 
- 
+ {{{
  public MathTrigFunctionsTest(String name) {
    super(name);
  }
+ }}}
  
- c) Create a setup and teardown class which make use of some of the BaseJDBCTestCase utilities
to get the connection and use a default database.
+  1.#3 Create a setup and teardown class which make use of some of the Base!JDBCTestCase
utilities to get the connection and use a default database.
- 
+ {{{
  ...
  
  private Connection conn;
@@ -666, +649 @@

    conn.rollback();
    conn.close();
  }
+ }}}
  
+  1.#4 Place your java class in the derby source tree in the appropriate directory according
to the package name.  For instance my Derby source directory is C:\derby_src\trunk\, so the
directory I put the !MathTrigFunctionsTest.java class is C:\derby_src\trunk\java\testing\org\apache\derbyTesting\functionTests\tests\lang

- d) Place your java class in the derby source tree in the appropriate 
- directory according to the package name.  For instance my Derby source
- directory is C:\derby_src\trunk\, so the
- directory I put the MathTrigFunctionsTest.java class is
- C:\derby_src\trunk\java\testing\org\apache\derbyTesting\functionTests\tests\lang 
  
+  1.#5 Make sure the Derby source classes directory, junit.jar and jakarta-oro-2.0.8.jar
files are in your class path.
- e) Make sure the Derby source classes directory, junit.jar and 
- jakarta-oro-2.0.8.jar files are in your class path.
  
+  1.#6 Edit the file derbylang.runall in the $DERBY_SRC/trunk/java/testing/org/apache/derbyTesting/functionTests/suites
directory with the name of your java class, but with a .junit extension, preceded by the directory
name where the class resides.  For instance with the !MathTrigFunctionsTest which resides
in the functionTests/tests/lang directory, this is the entry in the derbylang.runall file:
+ {{{
- f) Edit the file derbylang.runall in the 
- $DERBY_SRC/trunk/java/testing/org/apache/derbyTesting/functionTests/suites 
- directory with the name of your java class, but with a .junit extension, preceded
- by the directory name where the class resides.  For instance with the MathTrigFunctionsTest

- which resides in the functionTests/tests/lang directory, this is the entry in the
- derbylang.runall file:
- 
  lang/MathTrigFunctionsTest.junit
+ }}}
  
- g) To test your class
+  1.#7 To test your class
+ {{{
  	ant clobber
  	ant all
  	java org.apache.derbyTesting.functionTests.harness.RunTest lang/MathTrigFunctionsTest.junit

- 
+ }}}
-    To run all the tests in the derbylang suite use this command:
+  1.#8 To run all the tests in the derbylang suite use this command:
- 
+ {{{
  	java org.apache.derbyTesting.functionTests.harness.RunSuite derbylang
+ }}}
  
+ == Resources ==
  
- 10. Resources
- *************
- 
- JUnit Docs: Installed with JUnit zip file, also available at JUnit:
+ [http://junit.sourceforge.net/ JUnit Docs]: Installed with JUnit zip file, also available
at JUnit.
- http://junit.sourceforge.net/
  
- JUnit FAQ:
- http://junit.sourceforge.net/doc/faq/faq.htm
+ [http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]
  
- The Derby wiki:
+ The Derby wiki
- DerbyJUnitTesting,
+  * DerbyJUnitTesting
- http://wiki.apache.org/db-derby/DerbyJUnitTesting
- 
- DerbyJunitTestConfiguration,a new page that is under construction 
+  * DerbyJunitTestConfiguration, a new page that is under construction 
- (as mentioned above Dan is trying to set up/change the way we do JUnit with Derby),
- http://wiki.apache.org/db-derby/DerbyJunitTestConfiguration
  
+ Note: This may invalidate some of the stuff above about using JUnit with the Derby test
harness. 
- Note: This will probably invalidate the stuff I have above about using
- JUnit with the Derby test harness. 
  
  Eclipse JUnit Getting Started
- Open Eclipse Help from the main menu item, select the Java Development User Guide book,

- Getting Started >> Basic tutorial, Writing and running JUnit tests.
  
+ Open Eclipse Help from the main menu item, select the Java Development User Guide book,
Getting Started >> Basic tutorial, Writing and running JUnit tests.
- There is additional information in here that I did not cover such as running
- a single test method from a class and creating suites.
  
- Eclipse JUnit Tutorial - Third Party
- http://open.ncsu.edu/se/tutorials/junit/
+ [http://open.ncsu.edu/se/tutorials/junit/ Eclipse JUnit Tutorial]
  

Mime
View raw message