How to run junit test case using ant?


JUnit is an open-source unit testing framework for java programmers. It is only used for unit testing. Integration testing is done by TestNG.

How to run junit test case using ant?

As we discussed all basic concepts of Apache ant, let us see the below example to run junit test case using ant.

Example explanation:

When we run the build.xml file, control first go on the project element and look for the default target. In our example mainTarget is the default target so control go on the mainTarget. Then control looks for those targets on which mainTarget depends. So first junit target will execute followed by makedir, compile, war and mainTarget. The junit task in the junit target is responsible for runing the junit test case.

To run multiple test cases use the below code:

<batchtest fork="yes" todir="${junit.tests}">
<fileset dir="${src.testdir}">
<include name="**/*Test*.java"/>
Where todir specifies the output directory for the test report files.

* This is simple java class containing division method.
* @author javawithease

public class DivisionTest {
//data members
int num1, num2;
//parameterised constructor
public DivisionTest(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
//division method
public int division() throws ArithmeticException{
return num1/num2;
import static org.junit.Assert.*;
import org.junit.Test;
* This is test case class for division method.
* @author javawithease

public class DivisionTestCase {
//DivisionTest class objects
DivisionTest divisionTest1 = new DivisionTest(10, 2);
DivisionTest divisionTest2 = new DivisionTest(10, 0);
//Test case for division
public void test() {
assertEquals(5, divisionTest1.division());
//Test case for expected ArithmeticException,
//As in this case ArithmeticException
// is the expected exception so JUnit
//will pass this unit test.
@Test(expected = ArithmeticException.class)
public void testException() {
assertEquals(5, divisionTest2.division());
<?xml version="1.0" encoding="UTF-8"?>
<!-- Set the project name, basedir and default target to be executed-->
<project name="Ant-Build-Project-Test" default="mainTarget" basedir=".">
<!-- Sets the properties here-->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<!-- Target for deleting the existing directories-->
<target name="clean">
<delete dir="${build.dir}" />
<!-- Target for creating the new directories-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<!-- Target for compiling the java code-->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<!-- Defualt target to run all targets-->
<target name="mainTarget" depends="compile">
<description>Main target</description>


Buildfile: D:\TestWorkspace\AntJUnitTest\build.xml
[junit] Running com.javawithease.testcase.DivisionTestCase
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Test com.javawithease.testcase.DivisionTestCase failed
Total time: 283 milliseconds

No comments: