Wednesday, 29 August 2012

JUnit Tests

Unit Tests:

Unit tests give developers the ability to isolate and test individual sections of code to validate it against expected results. They are useful to verify code while development and when updated to ensure correctness.

In this blog we will be looking at JUit 4.X with maven. To include the JUnit test dependency in your POM  you need to include:


This will allow you to include JUint with your project. To use JUnit tests you will need to import at least these basics:

import org.junit.Assert;
import org.junit.Test;

JUint uses annotations to define tests and methods used to setup and cleanup after tests. So you can annotate multiple methods with each of these to have many tests and multiple setup or clean up functions.

Defines the method to run for a test.
Ignores a test case
Executes the method before each test to setup any data or resources
Executes the method after each tests to cleanup and data or resources
Executes the method once before all tests cases in this class. Can be used to setup systems that can be used for all tests to save time. This method must be static.
Executes the method once after all tests cases in this class, to clean up any resources once the testing of this class is complete. This method must be static.

The @test annotation can have two optional parameters. the first being the type of exception that is expected. If the exception is not thrown, the test case will fail. 


the second option parameter is a timeout, the test will fail if it takes longer then the timeout.


First we will declare a simple class we want to test. This just has one function that returns the value 10.

public class ClassToTest {

       public int functionTotest() {
              return 10;

Now we want to declare our test cases. First we need to create our class to hold our test case, There isn't anything specific needed to declare a class as holding test cases.

       public class MyTestCase {

We need to annotate our methods to define our unit tests. First we need to setup our tests cases, we can annotate a method with @Before to use it to setup our classes. This will execute this method once before all tests cases
       private ClassToTest clsToTest;
       public void beforeTest() {
              clsToTest = new ClassToTest();

To declare a test case we annotate a method with @Test
       public void testCase1() {

We can use Assert.* to validate our data. If an assertion is failed it will fail the test case

              Assert.assertTrue(clsToTest.functionToTest() == 10);

To run our test cases we open the class with the test cases. Then in eclipse we select from the menu, Run -> Debug as -> JUint Test

This will also open the results tab that will give you a break down on the test case results.

 That is enough to setup most test cases, but for more documentation see


Maven will automatically run test cases as long as they are located in 


to run tests with maven you just execute

         mvn test

Your test cases will run by default with maven builds. However, You may not always want to run test case because some can take a long time. You can disabled or skip tests using
mvn install -Dmaven.test.skip=true

No comments:

Post a Comment