Next page | Contents page |

Testing with JUnit

JUnit is a free product from www.junit.org.

It is also provided as a plug-in for IDEs, for quickly creating an outline JUnit class for testing your own class.

Corresponding to each class X to be tested you create a new class called XTest, in the same package but the package goes under a testsrc folder rather than the src folder. In that way the tests can be kept out of the deployment of the finished application but by being in the same Java package as the class under test, the tests can access all fields and methods except those declared as private. That should be sufficient to enable the tests to exercise all source lines.

NB: Versions

JUnit 3.x is used with code written in Java 1.4.x or earlier.

JUnit 4.x is rather different, using annotations, and requires Java 5+.

This course will only cover version 4.x.

Structure of a test class

package same.package.as.classx;

import org.junit.*;
import static org.junit.Assert;

public class XTest  // where X is name of class to be tested
{
	private X x;

	// Before each test method is called
	@Before
	public void setUp ()
	{
		x = new X (); 
	}

	// After each test method has run
	@After 
	public void tearDown () 
	{
		x = null; 
	}

	@Test 
	public void testThatXyzCausesAbc () 
	{
		String expected = "abc";
		assertEquals ("Should have been " + expected, expected, x.m ("xyz"));
	}
	
} // XTest

The first import is needed for the annotations. Any method annotated with @Test will be executed as a separate test.

Make the names of the test methods descriptive of the tests, so no javadoc-style explanatory comment is needed.

The second (static) import is for the assertions.

Assertion methods

Available assertions are static methods in class junit.framework.Assert (v3.8) or org.junit.Assert (v4+):

assertEquals (expected, actual) // with overloaded variants for all base types & String
assertTrue (boolean condition)
assertFalse (boolean condition)
assertNull (Object actual)
assertNotNull (Object actual)
assertSame (Object expected, Object actual)
assertNotSame (Object expected, Object actual)
fail ()

All have overloaded variants with extra first parameter: String failureMessage.

Methods annotated with @Before & @After

If the same objects are needed for every test make them fields of the test class, create them in an @Before method and set them to null in an @After method. Similar considerations apply to opening/closing files, database connections, etc, though generally those should be mocked out (see later) for unit testing.

It is particularly important when many unit test classes are run as part of a suite of tests that resources are fully released by each test class. Otherwise one test class may appear to be fine by itself but fail when part of a suite.

Running JUnit from the command line

set classpath=.;junit-4.4.jar
java org.junit.runner.JUnitCore full.name.of.MyClass

The response is of the form:

JUnit version 4.4
test output, if any

Time: 0.156

OK (1 test)

Running JUnit in NetBeans

Simply right click on the test file/suite and select "Run file". Results appear in a test window at the bottom of the screen.

If JUnit 4 is not set up, right click on the project and select Properties.

Next page | Contents page |