Ant - Junit Integration


Junit is the commonly used unit testing framework for development based on Java. It is easy to use and easy to extend. There are a number of JUnit extensions available. If you are unfamiliar with Junit, you should download junit from and read the junit manual.

This tutorial discusses about executing the junit tests using Ant. Ant makes this straight forward through the Junit task.

Presented below are the attribute of the junit task.

dir Where to invoke the VM from. This is ignored when fork is disabled.
jvm Command used to invoke the JVM. This is ignored when fork is disabled.
fork Runs the test in a separate JVM
errorproperty The name of the property to set if there is a Junit error
failureproperty The name of the property to set if there is a Junit failure
haltonerror Stops execution when a test error occurs
haltonfailureStops execution when a failure occurs
printsummaryAdvices Ant to display simple statistics for each test
showoutputAdivces Ant tosend the output to its logs and formatters
tempdir Path to the temporary file that Ant will use
timeout Exits the tests that take longer to run than this setting (in milliseconds).

Let us continue the theme of the Hello World Fax web applicaiton and add a junit target.

The below example shows a simple junit test execution

<target name="unittest">
  <junit haltonfailure="true" printsummary="true">
    <test name="com.tutorialspoint.UtilsTest"/>

The above example shows the execution of Junit on the com.tutorialspoint.UtilsTest junit class. Running the above will produce the following output

[echo] Testing the application
[junit] Running com.tutorialspoint.UtilsTest
[junit] Tests run: 12, Failures: 0, Errors: 0, Time elapsed: 16.2 sec