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:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>

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.


@Test
Defines the method to run for a test.
@Ignore
Ignores a test case
@Before
Executes the method before each test to setup any data or resources
@After
Executes the method after each tests to cleanup and data or resources
@BeforeClass
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.
@AfterClass
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. 

        @Test(expected=NullPointerException.class)

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

        @Test(timeout=100)

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.

       package com.cred.industries.platform.test;
       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;
       @Before
       public void beforeTest() {
             
              clsToTest = new ClassToTest();
       }

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

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

              Assert.assertNotNull(clsToTest);
              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 http://www.junit.org/

Maven:

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

      src/test/java
      src/test/resources

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