Start Search Contents Index Links About

Introduction to JUnit

Creating basic JUnit 4.0 test

2007 - Week 30 - Havard Rast Blok

This article marks the first in a series here at Remember Java which will look at technologies outside the core Java API. One such very relevant technology is JUnit for creating white box unit testing for your Java code. This and future articles will focus on JUnit 4.x, which requires Java JDK 5.0 or later. JUnit 4.x uses Java annotations , for marking various aspects of the test cases. JUnit is a Free and Open Source project, and junit.org is the official web site, where you can download the Jar you need for free.

The basic philosophy in unit testing, is to test your code at a very detailed level, often focusing on single methods of your classes and verify that they return or behave in the expected way. "Expected" is the keyword to most unit testing, since the tests often takes the for of 1) run method, 2) collect return value, and 3) compare to expected value. The expected value will be determined by separate way, or simple be predefined in the test.

public class MathTest{

	@Test
	public void testMax() {
		int a = 3; 
		int b = 4;
		int out = Math.max(a, b);
		
		assertEquals(b, out);
	}
}
			
So there it is, probably the most basic JUnit test you can create. There are a few key points to be aware of in this code:
  • By convention, the name of the class ends with Test.
  • The @Test is a Java annotation, indicating that this is a JUnit test method.
  • Also by convention, the name of the method is the same as the method under test. In the code above the method under test is max, an the JUnit method is testMax.
  • The test is self contained, which means it does not rely on any other tests to pass, and also, no other methods will fail if this test fails.
  • The last line assertEqualsis the actual check to see if the max-method returned its expected result. It takes two arguments, first the expected result, and then the actual output from the method under test.

In very many cases, the example above can be duplicated to all the other methods of the class under test, and you have a full test case for that class. However, in some cases, it is necessary to do carry out some initialization before each test, or before full test class. Likewise, it might be necessary to to certain clean-up steps at the end, like deleting temporary files, etc. In the code below, four more annotations are shown, which takes care of this.

public class MathTest {
	@BeforeClass
	public static void oneTimeSetUp() {
		System.out.println("Run once before all the tests");
	}

	@AfterClass
	public static void oneTimeTearDown() {
		System.out.println("Run once after all the tests");
	}

	@Before
	public void setUp() {
		System.out.println("Run once before each test");
	}

	@After
	public void tearDown() {
		System.out.println("Run once after each test");
	}
	
	@Test
	public void testMax() {
		int a = 3; 
		int b = 4;
		int out = Math.max(a, b);
		
		assertEquals(b, out);
	}
	
	@Test
	public void testMin() {
		int a = 3; 
		int b = 4;
		int out = Math.min(a, b);
		
		assertEquals(a, out);
	}
}
				


MathTest.java

The output from the test class above, will be as follows:

Run once before all the tests
Run once before each test
Run once after each test
Run once before each test
Run once after each test
Run once after all the tests
			



site: Håvard Rast Blok
mail:
updated: 27 July 2007