Writing Unit Tests with JUnit

What and Why?


JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.  Most programmers use only informal test methods.We think about the various cases to test for and we perform them manually by interacting with the unit/module. The problem with this approach is that it is very tedious to do and is also very repetitive. Students generally don’t like to test because of the boring and tedious repetition. By automating the process, testing becomes less painful, more reliable, and less time consuming. Furthermore, by using a framework, the process of writing the test case is as easy as following a recipe.
 

Setting Up your Environment


Download the JUnit Jar file:  junit.jar  (version 3.8)
Make your classpath variable point to junit.jar.  For example, if the jar file is located in a directory named C:\junit3.8:
(Windows) set classpath=%classpath%;C:\junit3.8\junit.jar;.  

(Alternately, you can provide the -classpath argument when you invoke the java command.)
 

Writing a Test Case


Let's write unit tests for the Rationals class from the Lewis and Loftus text. You can read the typical, cumbersome approach to testing in his RationalNumbers class on page 196.
 
 

  1. Download and compile Rational.java.
  2. Create a new java source file for the unit tests, RationalTest.java
  3. import junit.framework.*; 

  4. Implement a class derived from TestCase, a base class provided by JUnit.
        public class RationalTest extends TestCase
  5. Be sure the test class belongs to the same package as that of the unit you wish to test*
  6. Create a constructor that takes a string and passes it to super
       public RationalTest (String name)
        {
        super (name);
        }
  7. Override the setUp method
    Instantiate any objects you wish to test here, these objects are referred to as your fixture. JUnit will automatically setup a fresh fixture for you using this method prior to running each test method!
         protected void setUp()
        {
         r1 = new Rational(6, 8);
         r2 = new Rational(1, 3);
        }
  8. Implement a parameter-less method for each aspect of the unit you wish to test.
    The general strategy is to perform some action on the variables in your fixture using the methods of the class under test, then verify the results using one of the JUnit assert methods.

     public void testEquals()

  9.  {
      assertEquals(r1,r1);
      assertEquals(r1, new Rational(3, 4));
      assertTrue(!r2.equals(r1));
     }
     Note: assertEquals() requires that the Rationals class have an overriden equals() method. All Java classes are derived from Object and have a default equals method that compares object addresses and not object values, hence the need to override the equals method.


    If you conform to the simple convention that your method names begin with ‘test’, JUnit will then automatically execute all the methods as a suite.  Alternatively, you can set up your own suite (see documentation).
Here is the completed RationalTest class. Compile it before continuing to the next step.

Running the Test Suite


JUnit provides a TestRunner tool to perform the actual run of the test. The TestRunner classes take a Test class and runs all its methods. It provides feedback via the asserts. TestRunner will report how many tests were successful, how many failed, and how long the tests took. The asserts will tell you where the error occurred and implicitly what the error was. If all the tests pass, you get a pretty green bar, otherwise you get a red bar.

To run the TestRunner:

java junit.swingui.TestRunner <your test class>
for example: java junit.swingui.TestRunner RationalTest
You can either specify the test class at the commandline or load it from within the GUI. The [your test class] parameter becomes optional.

There is also a text version of the TestRunner, junit.textui.TestRunner, which is preferable for use in batch testing. For example:

java -cp .:/usr/local/bluej/lib/junit.jar junit.textui.TestRunner <your test class>

References

JUnit Primer
JUnit How-To
Test Infected: Programmers Love Writing Tests
Another helpful reference is the Assert class javadocs.



CPE 205 home

* This will give your test case access to "package private" members.