Introduction JUnit is a freely available set of Java classes that facilitate the unit testing of Java code. This document briefly introduces JUnit, and describes further resources related to JUnit. What is JUnit? - JUnit is a set of Java packages that allow one to readily create Java test cases for Java classes, and to then run these unit tests interactively or in batch mode. This hopefully encourages programmers to write unit tests for their code before they write the actual code. The intended result is higher quality code, as well as avoidance of the dreary task of going back and reconstructing unit tests after all code has been written.
- Two user interfaces are supported, text and GUI. The text user interface requires test case names to be passed in using a command line interface. The GUI version can be used to type a desired test case name into a window. It is also possible to run a preconstructed test suite from the GUI version, though I did not investigate this.
A quick view of JUnit in action The following shows the basics of using JUnit: First, we create a simple class that implement changes between different types of currency. The class, Money, has two fields, a constructor, two accessor methods for the fields, and two methods named add and equals. public class Money { private int fAmount; private String fCurrency; public Money (int amount, String currency) { fAmount = amount; fCurrency = currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; } public Money add (Money m) { return new Money (amount() + m.amount(), currency()); } public boolean equals (Object anObject) { if ( !(anObject instanceof Money) ) return false; Money aMoney = (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } } Next, we write the test cases for the two non-accessor methods using the following code: import test.framework.*; // this imports JUnit public class MoneyTest extends TestCase { // default constructor public MoneyTest(String name) { super(name); } // how to run the text based version of test runner public static void main(String args[]) { test.textui.TestRunner.run(suite()); } // A test for the Money.equals() method. // Tests usually initialize the object they are testing, // and then call one of two functions: // assert(boolean), // which fails if the boolean condition is false, and // assertEquals(object1, object2) // which fails if the two objects are not equal. public void testEquals() { Money m12CHF= new Money (12, "CHF"); Money m14CHF= new Money (14, "CHF"); assert(!m12CHF.equals (null)); assertEquals (m12CHF, m12CHF); assertEquals (m12CHF, new Money(12, "CHF")); // (1) assert ( !m12CHF.equals(m14CHF)); } // A test for the Money.add() method. public void testAdd() { Money m12CHF= new Money(12, "CHF"); // (1) Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); // (2) assert(expected.equals(result)); // (3) } // Finally, here is an example test suite // (a test suite is a group of test cases). // Each test case is added to the suite by passing // the name of the method that implements the test case // to the TestSuite.addTest method. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals")); suite.addTest(new MoneyTest("testAdd")); return suite; } /* initial code that will automatically be run at start of each test */ protected void setUp() { // there is no initialization code for this simple example, // though we considered making m14CHF an instance variable which // could then be initialized in this function. } } Finally, here is the output of the above program: .. Time: 0.70 OK (2 tests) Bugs - You must explicitly declare your test methods as public, or a MethodNotFound Exception will occur.
Observations - JUnit appears appropriate for supporting the creation of unit test cases at the same time we write code (at least for non user interface java code).
- The GUI version of JUnit may be most useful in running a small number of ad hoc tests. The text-based user interface appears more suitable for running a number of unit tests in batch mode.
About JUnit JUnit was written by Kent Beck and Erich Gamma, and is in the public domain. The Java source code can be found here. Annotated bibiography - Beck and Gamma (undated) Test Infected: Programmers Love Writing Tests. http://members.pingnet.ch/gamma/junit.htm. This document describes the benefits (and joys!) of a programming style in which test cases are written at the same time as is program code
|