In this Ant tutorial installment, we’ll take a look at the JUnit task in ant. The JUnit task is moderately complex, so you’ll definitely want to read the Ant manual. I would also recommend taking a look at Java Development with Ant by Erik Hatcher and Steve Loughran (this link will allow you to download Chapter 4: Testing with JUnit). to complete your installation of JUnit. Finally, this example was developed using version 1.5.1 of Ant and version 3.8.1 of JUnit.
We’ll start with the test class shown below. For simplicity, we’ll use a test case that always succeeds and doesn’t really test anything. We’ll take a closer look at writing JUnit test cases later. For now we’ll just say that a JUnit test case extends the junit.framework.TestCase class. Please create this file in a sub-directory named test (or change the value of the build file tst-dir property to suit).
And here is the build.xml file that we’ll be using. Please modify this build file to reflect the location of junit.jar on your system. In addition, you must add junit.jar to your CLASSPATH or copy junit.jar to your Ant library from your JUnit library to enable the integration of JUnit and Ant.
There are three primary differences between this build.xml file and the ones used previously:
Addition of the TALK property. We’ll use this as a flag to the verbose attribute of various tasks. Our example below will demonstrate the effect of the verbose attribute for the delete and the javac tasks. Note that we assign the property with the value attribute since this is not a file system location. This is different from the ant -verbose option. Setting verbose=true on a task causes that task to be more informative about it’s behavior. Passing the -verbose option to ant causes ant to be more informative about ant’s behavior.
Explicit declaration of the Java Classpath. In this case we define a base classpath and then we define the test classpath relative to it. If we were actually testing a real class, then the base classpath would be used to compile that class. The test classpath adds the location of the JUnit tests and the JUnit jar file to the base classpath.
The JUnit task. There are three nested elements to our JUnit task:
- The java classpath.
- The formatter, which controls the output from the task.
- The name, which defines a single test class to be run.
We can now execute our test as shown below.
$ ant -DTALK=false clean Buildfile: build.xml clean-compile-test: [delete] Deleting 1 files from /Tutorial/Ant/Junit/test clean: BUILD SUCCESSFUL Total time: 2 seconds $ ant -DTALK=false Buildfile: build.xml compile-test: [javac] Compiling 1 source file test: [junit] Testsuite: TestExample [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.007 sec all: BUILD SUCCESSFUL Total time: 4 seconds $ ant clean Buildfile: build.xml clean-compile-test: [delete] Deleting 1 files from /Tutorial/Ant/Junit/test [delete] Deleting /Tutorial/Ant/Junit/test/TestExample.class clean: BUILD SUCCESSFUL Total time: 2 seconds $ ant Buildfile: build.xml compile-test: [javac] Compiling 1 source file [javac] [parsing started /Tutorial/Ant/Junit/test/TestExample.java] [javac] [parsing completed 81ms] [javac] [loading /Projects/Java/Lib/junit.jar(junit/framework/TestCase.class)] [javac] [checking TestExample] [javac] [loading /Projects/Java/Lib/junit.jar(junit/framework/Assert.class)] [javac] [loading /System/Library/.../1.3.1/Classes/classes.jar(java/lang/Object.class)] [javac] [loading /Projects/Java/Lib/junit.jar(junit/framework/Test.class)] [javac] [loading /System/Library/.../1.3.1/Classes/classes.jar(java/lang/String.class)] [javac] [wrote /Tutorial/Ant/Junit/test/TestExample.class] [javac] [total 866ms] test: [junit] Testsuite: TestExample [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.005 sec all: BUILD SUCCESSFUL Total time: 4 seconds
Disclaimer: I don’t claim to be an expert on ant. Please send comments and corrections.