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 21:55:03 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

------------------------------------------------------------------------------
- =An Introduction to JUnit=
+ = An Introduction to JUnit =
  
- ==Topics==
+ == Topics ==
  
   1 Introduction to JUnit
   2 Where to get JUnit
@@ -16, +16 @@

   10 Resources
  
  
- ==Introduction to JUnit==
+ == Introduction to JUnit ==
  
  Writing tests during development
  Code a little, test a little, ad infinitum
@@ -35, +35 @@

  More info: JUnit Test Infected: Programmers Love Writing Tests
  In the installed documentation: junit3.8.2/doc/testinfected/testing.htm
  
- ==Where to get JUnit==
+ == Where to get JUnit ==
  
  http://prdownloads.sourceforge.net/junit/
  The Derby tests have been run against 3.8.1 
@@ -47, +47 @@

  Example of 3.8.2 improvement:
  expected:<...st of the emergency [broadcasting] system> but was:<...st of the emergency
[locating] system> 
  
- ==Running the Examples==
+ == Running the Examples ==
  
+ {{{
  %JUNIT_HOME%/junit/samples
  Add junit.jar to your class path 
  
@@ -63, +64 @@

  Time: 0.811
  
  OK (131 tests)
+ }}}
  
- ==JUnit API== 
+ == JUnit API == 
  
  junit.framework.Test Interface has two methods
  	countTestCases() - counts the number of test cases this test (java class) will run
@@ -88, +90 @@

  Your class must extend junit.framework.TestCase
  	
  
- ==Creating your First Test (Command Line and Eclipse)==
+ == Creating your First Test (Command Line and Eclipse) ==
  
   1 Implement a subclass of TestCase (extend TestCase)
   2 Override the method runTest(), calling the tests to run
@@ -119, +121 @@

  }
  
  }}}
- ====To test this class via the Command Line====
+ ==== To test this class via the Command Line ====
  
   1 make sure junit.jar is in your class path and the directory where the
  	compiled classes reside
@@ -139, +141 @@

  OK (1 test) 
  
  
- ====To create and test this class via Eclipse====
+ ==== 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
@@ -160, +162 @@

  	Errors or Failures. 
  
  
- ==Fixtures==
+ == Fixtures ==
  
  The preceding example ran one test in one test class.  It's not very efficient
  to build a test like that so let's create a Fixture of tests next.
@@ -192, +194 @@

  instead of having to use the tool, junit.textui.TestRunner class, for our main
  class. 
  
- ====JUnit Fixture class with a Main method====
+ ==== JUnit Fixture class with a Main method ====
- 
+ {{{
  package junitdemo;
  
  import junit.framework.TestCase;
@@ -249, +251 @@

  	}
  
  }
- 
+ }}}
  To run this class via Eclipse select Run As >> Java Application.
  
  To run this class via the Command Line:
@@ -263, +265 @@

  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):
- 
+ {{{
  C:\junit\junit3.8.2>java junitdemo.FixtureJUnitTest
  .....F
  Time: 0
@@ -283, +285 @@

  
  FAILURES!!!
  Tests run: 5,  Failures: 1,  Errors: 0
- 
+ }}}
  
  Looking at the output you can see that our test called testStringSameObject2
  failed with our custom message: Fail: string1 is not the same as s2.
@@ -295, +297 @@

  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 FixtureJUnitTest
- 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 ==
- 7. Suites
- *********
  
  A TestSuite is the smallest execution unit in JUnit.   
  
  To run all of the tests in a class do the following:
  
- a) Create a one arg constructor which takes a String - the name of the method to run
+  1. Create a one arg constructor which takes a String - the name of the method to run
- b) 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
  
  The class below, SuiteJUnitTest.java demonstrates this.
  
- Running a Suite of Tests
+ ==== Running a Suite of Tests ====
+ {{{
- ------------------------
- 
  package junitdemo;
  
  import junit.framework.Test;
@@ -380, +380 @@

  	}
  
  }
- 
+ }}}
  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() {
  		TestSuite suite = new TestSuite();
@@ -392, +392 @@

  		suite.addTest(new SuiteJUnitTest("testStringEqual1"));
  		return suite;
  	}
- 
+ }}}
  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
  ..
  Time: 0
  
  OK (2 tests)
+ }}}
  
- Creating Suites from different Java classes 
+ ==== Creating Suites from different Java classes ====
- -------------------------------------------
  
  You can also create a class that acts as sort of a harness that
  runs suites from two or more classes.  An example of this is shown
@@ -411, +411 @@

  from other classes, you could have one class that adds tests from it's
  own suite and adds tests from another class too.
  
+ {{{
  package junitdemo;
  
  import junit.framework.Test;
@@ -437, +438 @@

  	
  }
  
+ }}}
  
- SuiteTest1JUnitTest
+ ===== SuiteTest1JUnitTest ====
- -------------------
  
  package junitdemo;
  
@@ -496, +497 @@

  
  }
  
+ }}}
  
- SuiteTest2JUnitTest
+ ===== SuiteTest2JUnitTest =====
- -------------------
  
+ {{{
  package junitdemo;
  
  import junit.framework.Test;
@@ -534, +536 @@

  	}
  
  }
+ }}}
  
- 8. More about Assertions
+ == More about Assertions ==
- ************************
  
  There are several assertXXX methods, and the examples so far have not
  used very many of them.  Check out the javadoc that comes with JUnit
@@ -555, +557 @@

  ------------
  
  Overloaded for almost all primitive data types and the Object arguments.
- 
+ {{{
  double expected = java.lang.StrictMath.acos(testArcValues[i]);
  double rValue = executeValues("ACOS", testArcValues[i]);
  assertEquals(expected, rValue, 0.0);
+ }}}
  
  assertFalse
  -----------
  
  Takes a boolean argument.  Could use in place of assertTrue(boolean)
- 
+ {{{
  int result = val1 + val2;
  assertFalse(result == 10);
+ }}}
  
  assertNotNull
  -------------
  
  Takes an object with an optional second argument, the String message.
- 
+ {{{
  s2 = new String("string1");
  assertNotNull("Fail: Object s2 is null. ", s2);
+ }}}
  
  assertNotSame
  -------------
  
  Takes two objects with an optional third argument, the String message.
- 
+ {{{
  s2 = new String("string1");
  assertNotSame("Fail: string1 is the same as s2", "string1", s2)
+ }}}
  
  assertNull
  ----------
  
  Takes an object with an optional second argument, the String message.
  
+ {{{
  Object nullObj = null;
  assertNull("Fail:object was not null", nullObj);
+ }}}
  
  assertTrue
  ----------
- 
+ {{{
  int val1 = 5;
  int val2 = 6;
  int result = val2 - val1;
  assertTrue(result == 1);
+ }}}
  
  fail
  ----
@@ -609, +618 @@

  action that should throw an exception and you don't know whether the exception
  was thrown or not.  Since JUnit does not output exceptions, just failures,
  use fail to fail the test if the exception was not thrown.
- 
+ {{{
  try {
       // for the value TWO_RADIANS, this method should throw
       // an SQLException in Derby
@@ -618, +627 @@

  					+ TWO_RADIANS);
      } catch (SQLException sqlE) {
         // "ERROR 22003: The resulting value is outside the range for the data type DOUBLE.";
+ }}}
  
- 9. JUnit tests and the Derby test harness
+ == JUnit tests and the Derby test harness ==
- *****************************************
  
+ I'm writing this section as someone who is new to the Derby test harness. (If someone who
is familiar with the test harness would like to edit this section, please do!)
- CAVEATS: 
- I don't know the Derby test harness
- Dan is changing the design of the JUnit suite stuff as we speak
  
- But, as of 8/28/ the below works for this one test ;-)
+ Also, I believe there will be some changes to the way the Derby test harness uses JUnit,
for more info refer to this page.
+ 
+ However, as of August 29th, 2006 this information is valid.
+ 
  
  a) Create a JUnit test that extends the Derby class BaseJDBCTestCase
  (org.apache.derbyTesting.junit.BaseJDBCTestCase) and put it in the correct 

Mime
View raw message