Running Test Cases

To run the JUnit test cases, add a test target to the build file introduced at the beginning of the chapter, and make the main target depend on the test target as part of the build process:

            <target name="main" depends="init, compile, test, compress, deploy">
        <echo>
            ${message}
        </echo>
    </target>

The test target will run the six targets you're going to create in this chapter:

    <property name="testsOK" value="Tested OK...." />
        .
        .
        .
    <target name="test" depends="test1, test2, test3, test4, test5, test6">
            <echo>
            ${testsOK}
            </echo>
            </target>

If you're not using Ant or a Java IDE, you usually run JUnit tests from the command line and use the junit.textui.TestRunner class like this, testing the example class created earlier in the chapter, org.antbook.Project:

%java junit.textui.TestRunner org.antbook.Project

You can do essentially the same thing in Ant using the java task, and that looks like this in the build file for the first test task, test1. Note that I'm adding junit.jar to the classpath:

    <target name="test1" depends="compile">
        <java fork="true" 
            classname="junit.textui.TestRunner" 
            classpath="${ant.home}/lib/junit.jar;.">
            <arg value="org.antbook.Project"/>
        </java>
    </target>

Here's what this task looks like when it's running:

test1:
     [java] ...
     [java] Time: 0.01

     [java] OK (3 tests)

Each dot (.) indicates a test case that's running, and three test cases are in the example. As you can see from the last line, the tests all passed OK, but this isn't exciting and it doesn't ...

Get Ant: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.