Skip to content
UoL CS Notes

Testing with JUnit & Ant

COMP285 Lectures

Testing with setup() & tearDown()

We use setUp() and tearDown() to ensure that the tests don’t interfere with each-other. We can implement this, in our testing class, like so:

package org.example.antbook.junit;
//import JUnit4 classes:
import static org.junit.Assert.*;
import org.junit.Test;     //
import org.junit.Before;   //import org.junit.* also would work
import org.junit.After;    //

public class setUpTearDownTest{
     @Before    //Runs before each @Test method
     public void setUp(){ System.out.println("setUp sets up a fixture"); }
     @After     //Runs after each @Test method
     public void tearDown(){ System.out.println("tearDown releases fixture"); }
     @Test
     public void testA(){ 
         System.out.println("testA runs"); 
         assertTrue("MULTIPLICATION FAILED!!!", 4 == (2 * 2));  
     }     
     @Test              //Each method annotated by @Test runs
     public void testB(){ 
         System.out.println("testB runs"); 
         assertSame("ADDITION FAILED!!!", 4, 2 + 2); 
     }    
     @Test 
     public void SomeTestC(){ 
         System.out.println("SomeTestC runs"); 
         assertSame("ADDITION FAILED!!!", 5, 2 + 2); 
     }
}

Tests won’t run if there is not an @Test annotation before them.

Running Tests

We can run tests from the command line like so:

java -cp build\test \
org.junit.runner.JUnitCore \
org.example.antbook.junit.setUpTearDownTest

You will need to have your code compiled before this.

Test Suites

We can run many JUnit test cases by grouping them into a suite:

package org.example.antbook;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(value=Suite.class)
@SuiteClasses(value=
	{
		org.example.antbook.junit.SimpleTest.class, org.example.antbook.junit.setUpTearDownTest.class,	org.eclipseguide.persistence.FilePersistenceServicesTest.class
	}
)
public class AllTests{}

Ant has its own method of grouping tests so we shouldn’t use this when using Ant.

<junit> in Ant

Ant has it’s own task for running a selection of tests for a set of files:

<target name="test-brief" depends="test-compile">
 <junit>
   <classpath refid="test.classpath"/>
   <test name="org.eclipseguide.persistence.FilePersistenceServicesTest"/>
   <test name="org.example.antbook.junit.SimpleTest"/>
 </junit>
</target>

We can provide more information and stop the build if the test fails using the following build file:

<target name="test-brief" depends="test-compile">
 <junit haltonfailure="false" printsummary="true"> 
   <classpath refid="test.classpath"/>
   <test name="org.eclipseguide.persistence.FilePersistenceServicesTest"/>
   <test name="org.example.antbook.junit.SimpleTest"/>
 </junit>
</target>

You may not want to use haltonfailure if you want to see all the tests that fail.

Directory Structure

Folder Description
ch04 Base directory basedir= "."
ch04\src Source directory ${src.dir}
ch04\test Test directory ${src.test.dir} containing deeper JUnit test classes.
ch04\build Build directory ${build.dir}
ch04\build\classes For compiled source files ${build.classes.dir}
ch04\build\test For compiled JUnit test cases ${build.test.dir}
ch04\build\data For test reports in XML format.
ch04\build\report For test reports in HTML format (new directories data and report to be considered later).

JUnit Build File Structure

When adding testing to an Ant build we can use the following structure:

  1. test-init Target - Initialise the testing directory structure with <mkdir> (the last three folders are automatically generated).
  2. test-compile Target - Compile the test code using <javac>.
  3. test Target - Execute the tests with <junit> or <java>.
  4. test-reports - Use <junitreport> and <report> to generate test reports.