Writing unit tests with JUnit 4 annotations


Starting with version 4, it is no longer necessary to extend class TestCase as in JUnit 3 (and before) to implement unit tests. Instead, you can use annotation to mark your POJO as a test class.

The basics

As an example, let say we want to write unit tests for the following class:

public class IntegerAdder {

public long add(int a, int b) {

if (a < 0 || b < 0) {

throw new IllegalArgumentException();


return a + b;



Now, we can create the POJO test class IntegerAdderTest:

public class IntegerAdderTest {


To initialize and clean up resources used by the class use the following annotations:

  • @Before – called to initialize before each test, equivalent to setUp() method.
  • @After – called to clean up after each test, equivalent to tearDown() method.
  • @BeforeClass – called once to initialize static resources before running all tests.
  • @AfterClass – called once to clean up after running all tests.

For example:


public void setUp() {

adder = new IntegerAdder();




public void tearDown() {




public static void setUpClass() {




public static void tearDownClass() {



To add an unit test, just write the test method and annotate it with @Test, for example:


public void testAddSucceed() {


assertEquals(0L, adder.add(0,0));

assertEquals(3L, adder.add(1,2));


@Test also comes with an “expected” attribute where you can use to write test case where an exception is expected. For example, the add() method in the IntegerAdder class would throw an IllegalArgumentException if any of its input arguments have negative values. The test method will hence be like this:

@Test(expected = IllegalArgumentException.class)

public void testAddFailed() {


adder.add(0, -1); // should throw an exception


Putting it all together, the following output will be generated:









Parameterized tests

In many cases, we would like to test a method with different arguments, e.g. to check that it is working for some boundary conditions. Instead of adding the method calls explicitly like in the testAddSucceed() method above, JUnit 4 allows you to setup a test class to run with multiple parameters. To do that:

  • Annotate test class with@RunWith tag

@RunWith (value = Parameterized.class)

public class IntegerAdderTest {…

  • Add class member to store data and create constructor with same number of arguments:

// Parameters

private int a;

private int b;

private long expectedSum;

public IntegerAdderTest(int a, int b, long sum) {

this.a = a;

this.b = b;

this.expectedSum = sum;


  • Add a static method to populate the parameters:


public static Collection<Object[]> data() {

Object[][] values = new Object[][] {

{0, 0, 0L},

{1, 2, 3L}


return Arrays.asList(values);


  • Write unit test methods using the parameters, for example:


public void testAddParameterized() {

System.out.println(“testAddParameterized (a, b, sum): ” + a + ” ” + b + ” ” + expectedSum);

assertEquals(expectedSum, adder.add(a, b));


The following output will be displayed when the test is run:



>testAddParameterized (a, b, sum): 0 0 0



>testAddParameterized (a, b, sum): 1 2 3



I have removed the test methods testAddSucced() and testAddFailed() above or they will be run multiple (2) times here. Note you can use @Ignore annotation on a test method to prevent it from running as well.


About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.