JUnit Ant Example

1. Introduction

In this tutorial, I’ll be showing an example of how to run your test units in Apache Ant. It is entirely possible that one of the applications you might encounter in your experience as a developer to use Apache Ant. After all, it was the defacto standard for building Java/Java EE apps before so the possibility of you using this tool to run your builds and test is still high up the ceiling.

Before we dive in though, let me just give you an overview what Apache Ant is.

Apache Ant as stated on their site is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

This tool can be used to drive the entire building process of your Java applications by invoking tasks that corresponds to the build instructions given by the builder (which is sometimes the deployer or the developer).

Given that, we can use it to execute our JUnit Tests as well from our application along with the build.

2. Setup

2.1 Download Ant

Let’s start by downloading and setting your ANT library on your environment classpath. You can download ANT from here. You can download the binary distribution and put it somewhere in your environment tools or path.

2.2 Set it on your classpath

Next, we set it on your classpath. In windows, you simply put the “bin” folder on your “PATH”.

Figure 1.0 Set ant in Windows classpath
Figure 1.0 Set ant in Windows classpath

In MAC you can put it in your bash file.

Figure 2.0 Set ant in MACOSX path
Figure 2.0 Set ant in MACOSX path

3. Run your test cases

Ant has a built-in task called “junit” which can run your unit tests. Here is a simple example:


<project name="junit-ant-example" default="unit-test-1" basedir=".">
	<property name="src" location="src" />
	<property name="build" location="build" />
	<property name="dist" location="dist" />

    simple example build file
	<target name="unit-test-1">
		<junit printsummary="yes" haltonfailure="yes">
				<pathelement location="${project.class.path}" />
				<pathelement location="${build.tests}" />
				<pathelement path="${java.class.path}" />
			<test name="com.areyes1.jgc.junit.assertequals.JUnitAssertEqualsExample" haltonfailure="no" outfile="result">
				<formatter type="plain" />
				<formatter type="xml" />

You’ll be able to run this via Eclipse or Command Line. In Eclipse you can right click on the build.xml > Run as > Ant build.. and select the unit-test-1 target. If you prefer to run it in the command line, you can call this:

ant -buildfile test.xml dist

4. Add on: Running Batch JUnit

You can also run a batch of JUnit Test via Ant. Here is an example ant XML for that.


	<target name="unit-test-2-batch">
		<mkdir dir="${reports.tests}" />
		<junit printsummary="yes" haltonfailure="yes">
				<pathelement location="${project.class.path}" />
				<pathelement location="${build.tests}" />
				<pathelement path="${java.class.path}" />

			<formatter type="plain" />
			<formatter type="xml" />

			<batchtest fork="yes" todir="${reports.tests}">
				<fileset dir="${src.test}">
					<include name="**/*Test*.java" />

5. Download the Eclipse project

This was an example of JUnit Ant

You can download the full source code of this example here: junit-ant-example

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button