How to run JUnit test suite from Ant

When I was developing performance test framework for wso2registry, I came across a problem of using Ant to run a Junit test suite. As I seen in the web, lots of users asking whether Ant supports for running Junit test Suite class. I didn't see anything mentioned in the documentation as to if this is possible. Here is my answer for all, running a Junit test Suite is possible using Ant. Following simple step by step guide illustrate how to do that.


As the first step, let's create a sub directory called “mytest” and put the bellow test class to the directory. For simplicity this test class doesn't really test anything.

import junit.framework.TestCase;
public class HelloWorld extends TestCase {

public HelloWorld(String text){
super (text);
}

public void testprint1(){
System.out.println("Hello World1");
assertTrue( "TestExample", true );
}

public void testprint2(){
System.out.println("Hello World2");
assertTrue( "TestExample", true );
}
}

Then create the below test suite in the same subdirectory. In JUnit, "suites" can be used to run a group of test classes and allows for some additional functionality.

import junit.framework.Test;
import junit.framework.TestSuite;

public class HelloworldSuite extends TestSuite {

public static Test suite(){
TestSuite suite = new TestSuite();

suite.addTest(new HelloWorld("testprint1"));
suite.addTest(new HelloWorld("testprint2"));
suite.addTest(new HelloWorld("testprint1"));

return suite;
}
}

Now create a directory called “lib” inside your “mytest” directory and copy the junit.jar file to it. In my case, I'm using junit-4.4.jar. In addition to that you need to add junit.jar to your CLASSPATH or copy junit.jar to your Ant library(ANT_HOME/lib) from your JUnit library to enable the integration of JUnit and Ant.


Now put the below build.xml file to your “mytest” directory.

<project name="Hello-world-sample" basedir="." default="test">

<property name="dest.dir" value="build" />
<property name="dest.dir.classes" value="${dest.dir}/classes" />
<property name="dest.dir.lib" value="${dest.dir}/lib" />
<property name="home" value="." />

<path id="build.class.path">
<fileset dir="${home}/lib">
<include name="*.jar" />
</fileset>
</path>

<path id="test.class.path">
<pathelement location="${dest.dir.classes}" />
</path>

<target name="clean">
<delete dir="${dest.dir}" />
</target>

<target name="prepare">
<mkdir dir="${dest.dir}" />
<mkdir dir="${dest.dir.classes}" />
</target>

<target name="compile" depends="clean,prepare">
<javac srcdir="src" destdir="${dest.dir.classes}">
<classpath refid="build.class.path" />
</javac>
</target>

<target name="run" depends="compile"/>

<target name="test" depends="compile">
<junit>
<classpath refid="test.class.path" />
<classpath refid="build.class.path" />
<formatter type="brief" usefile="false" />
<test name="HelloworldSuite" />
</junit>
</target>
</project> 
Finally execute the test as below.

C:\Ant_Test>ant test
Buildfile: build.xml

clean:
[delete] Deleting directory C:\Ant_Test\build

prepare:
[mkdir] Created dir: C:\Ant_Test\build
[mkdir] Created dir: C:\Ant_Test\build\classes

compile:
[javac] Compiling 4 source files to C:\Ant_Test\build\classes

test:
[junit] Testsuite: HelloworldSuite
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] inside test print1
[junit] inside test print2
[junit] inside test print3
[junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 1 second

Note that if you use packages then the test name in the build.xml should be change as

<test name="package.name/HelloworldSuite"/>