JUnit

Ref:

What is JUnit?

JUnit is a simple, open source framework to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:

  • Assertions for testing expected results
  • Test fixtures for sharing common test data
  • Test runners for running tests

When should tests be written?

Tests should be written before the code. Test-first programming is practiced by only writing new code when an automated test is failing.

Good tests tell you how to best design the system for its intended use. They effectively communicate in an executable format how to use the software. They also prevent tendencies to over-build the system based on speculation. When all the tests pass, you know you're done!

Whenever a customer test fails or a bug is reported, first write the necessary unit test(s) to expose the bug(s), then fix them. This makes it almost impossible for that particular bug to resurface later.

Test-driven development is a lot more fun than writing tests after the code seems to be working. Give it a try!

Basic test template

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

public class SampleTest {

private java.util.List emptyList;

/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
@Before
public void setUp() {
emptyList = new java.util.ArrayList();
}

/**
* Tears down the test fixture.
* (Called after every test case method.)
*/
@After
public void tearDown() {
emptyList = null;
}

@Test
public void testSomeBehavior() {
assertEquals("Empty list should have 0 elements", 0, emptyList.size());
}

@Test(expected=IndexOutOfBoundsException.class)
public void testForException() {
Object o = emptyList.get(0);
}
}

Handling Exception

* How test that passes when an expected exception is thrown?

Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:

    @Test(expected=IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {
ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);
}

* How test that fails when an unexpected exception is thrown?

Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.

The following is an example test that fails when the IndexOutOfBoundsException is raised:

    @Test
public void testIndexOutOfBoundsExceptionNotRaised()
throws IndexOutOfBoundsException {

ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);
}

Assertion Statement Reference

This is a list of the different types of assertion statements that are used to test your code. Any Java data type or object can be used in the statement. These assertions are taken from the JUnit API.

  • assertEquals(expected, actual)
  • assertEquals(message, expected, actual)
  • assertEquals(expected, actual, delta) – used on doubles or floats, where delta is the difference in precision
  • assertEquals(message, expected, actual, delta) – used on doubles or floats, where delta is the difference in precision
  • assertFalse(condition)
  • assertFalse(message, condition)
  • assertNotNull(object)
  • assertNotNull(message, object)
  • assertNotSame(expected, actual)
  • assertNotSame(message, expected, actual)
  • assertNull(object)
  • assertNull(message, object)
  • assertSame(expected, actual)
  • assertSame(message, expected, actual)
  • assertTrue(condition)
  • assertTrue(message, condition)
  • fail()
  • fail(message)
  • failNotEquals(message, expected, actual)
  • failNotSame(message, expected, actual)
  • failSame(message)

Ref:

Pre and Post condition

  • A pre-condition is an expression of type Boolean that should always hold before the execution of the body of the method,conditions for each function that must be true for it to behave correctly
  • A post-condition is an expression of type Boolean that should always hold after the execution of the body of the method,conditional expressions that must be true when a method returns
  • The strategy is to construct a JUnit test case for each accessible method in the class under test, where each test case verifies both the preconditions and postconditions of the method. Example
    public class RangeTest extends TestCase {  
    private Range mRange;
    public RangeTest(String name) {
    super(name);
    }

    protected void setUp() throws Exception {
    super.setUp();
    mRange = new Range(1, 5);
    }

    public void testConstructor() {
    // pre: aLower < aUpper
    try {
    new Range(6, 4);
    fail("[6,4] should be precluded");
    }
    catch (AssertionError e) { }
    // pre: aLower == aUpper new Range(-3, -3);
    // post: aLower == getLower()
    Assert.assertEquals("[1,5].getLower() should be 1", 1,
    mRange.getLower());
    // post: aUpper == getUpper()
    Assert.assertEquals("[1,5].getUpper() should be 5", 5,
    mRange.getUpper());
    }

    public void testGetLower() {
    // pre: object constructed
    Assert.assertNotNull("[1,5] could not be constructed", mRange);
    // post: getLower() == aLower provided to c'tor
    Assert.assertEquals("[1,5].getLower() should be 1", 1,
    mRange.getLower());
    }

    public void testGetUpper() {
    // pre: object constructed
    Assert.assertNotNull("[1,5] could not be constructed",
    mRange);
    // post: getUpper() == aUpper provided to c'tor
    Assert.assertEquals("[1,5].getUpper() should be 5", 5,
    mRange.getUpper());
    }

    public void testIntersects() {
    // pre: anOther != null
    try {
    mRange.intersects(null);
    fail("intersects(null) should be precluded");
    }
    catch (AssertionError e) { }
    // post: true if two ranges have an integer in common
    Assert.assertTrue(!mRange.intersects(new Range(-3, -1)));
    Assert.assertTrue(!mRange.intersects(new Range(6, 7)));
    Assert.assertTrue(!mRange.intersects(new Range(-2, 1)));
    Assert.assertTrue(!mRange.intersects(new Range(5, 7)));
    Assert.assertTrue(mRange.intersects(new Range(0, 2)));
    Assert.assertTrue(mRange.intersects(new Range(3, 4)));
    Assert.assertTrue(mRange.intersects(new Range(4, 8)));
    }}

  • If these test cases pass, then we can be confident that all methods in Range are:
  • Checking for violation of their preconditions (and objecting by throwing a Runtime exception of some sort)
  • Adhering to their postconditions.

Ref: http://www.hacknot.info/hacknot/action/showEntry?eid=17

Ant: Simple JUnit Test

This is the final part of the so called Ant series I have been writing in this blog, about Apache Ant. This is an extremely brief tutorial on JUnit. The goal is to test Ant and JUnit framework integration .

The previous posts on Ant can be found here:

  1. Apache Ant
  2. Ant: Installation
  3. Ant: Hello World in Ant
  4. Ant: Java Hello World in Ant
  5. Ant: Java Swing Hello World in Ant

Prerequisites

In order to do this we will need to have a Java compiler, Ant, and JUnit installed.

Java Classes

First, we need a Java class to test. I had to make this example too simple according to my Java knowledge. This lacks any Java Docs or comments, but it's a pretty simple bit of code. 🙂

public class Math {
static public int add(int a, int b) {
return a + b;
}

static public int multiply ( int a, int b) {
return a * b;
}
}

Then to test this code, we need a second Java class that will

  1. import junit.framework.*
  2. extend TestCase

There are two important points to note in the sample. First, the routine is named testAdd. This convention tells that the routine is supposed to be a test and that it's targeting the add functionality. Here's the matching example.

import junit.framework.*;

public class TestMath extends TestCase {

protected void setUp() {
// put common setup code in here
}

protected void tearDown() {
// put common cleanup code in here



}

public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}

public void testMulitply() {



int num1 = 3;
int num2 = 7;
int total = 21;
int sum = 0;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);

num1 = 5;
num2 = 4;



total = 20;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);
}
}

And both these files could be kept in a sub-folder named src

Ant Script

The last step is how to run your JUnit tests using Ant. Create a file called build.xml and place it in the main folder. The core of this scripts is the following, which does the JUnit tests.

<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">



<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}">



<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>

The file I used is this one. (The inner details of this scripts are Ant Scripting and XML, which is not explained in this document, but really straightforward to understand)

Output

Run ant test in command line:

[root@nimal junit-sample]# ant test
Buildfile: build.xml

init:

compile:
[javac] Compiling 2 source files to /opt/junit-sample/bin

run-tests:
[junit] Running TestMath

[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.321 sec

test:
[junitreport] Processing /opt/junit-sample/reports/TESTS-TestSuites.xml to /tmp/null1227203872
[junitreport] Loading stylesheet jar:file:/usr/local/ant/lib/ant-junit.jar!/org/apache/tools/ant



/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 1714ms
[junitreport] Deleting: /tmp/null1227203872

BUILD SUCCESSFUL
Total time: 6 seconds

Ant will also do nice things like create nice HTML reports! I've linked to a simple Ant script that will compile all the code in your "src" folder, run all the tests named "test*" and then create an HTML report in your "reports\html" folder.

I've linked to a zip file (ant-junit-sample.zip) that contains the build.xml file, the source code and the test class. (If you try to run it and get errors about JUnit not being found, remember to add that junit.jar to your Ant lib folder.)

Attachments

  • ant-junit-sample.zip (109.6 kB) –Scripts for Test ,
  • build.xml (2.7 kB) –Ant+JUnit build script ,