Chapter 10. Using JUnit with Ant

The Ant <junit> target is non-graphical. (It's packed in the Optional Ant jar, not with the core tasks, but that usually doesn't matter since the optional jar file will always be in Ant's classpath unless somebody has monkeyed with the Ant distribution). You can, of course, invoke a graphical Test Runner using the <java> task, but be aware that the Test Runner gui never works from my Ant JVM. I set fork='true' and then it works fine. Here are some samples showing how to invoke JUnit tests in various ways. The gui test invokes my TestCase using the main() method that I described above. The non-gui test runs a batch of test suites, but has a commented-out line showing how to invoke a single test suite.

    
    <!-- This one runs the designated TestCase suite in a Gui -->
    <target name='guitest' depends='-test-shared, compile-tests'
            description='Run Unit Tests in the Junit Swing Gui'>
        <!-- note the fork='true' in the following <java> task -->
        <java classname='com.admc.jamama.smtp.SMTPTest' fork='true'>
            <classpath>
                <pathelement path='${jar.junit}'/>
                <pathelement path='testclasses'/>
                <pathelement path='classes'/>
                <fileset dir='lib'/>
            </classpath>
            <!-- the following stuff required by my specific application -->
            <arg value='-g'/>
            <sysproperty key='test.targetserver' value='${test.targetserver}'/>
            <sysproperty key='test.targetport' value='${test.targetport}'/>
        </java>
    </target>

    <!-- This one runs all of the designated TestCases non-graphically -->
    <target name='test' depends='-test-shared, compile-tests'
            description='Run unit tests in non-graphical mode'>
        <junit printsummary='true' haltonfailure='true'>
            <formatter type='brief' usefile='false'/>
            <classpath>
                <pathelement path='${jar.junit}'/>
                <pathelement path='testclasses'/>
                <pathelement path='classes'/>
                <fileset dir='lib'/>
            </classpath>
            <!-- the following stuff required by my specific application -->
            <sysproperty key='test.targetserver' value='${test.targetserver}'/>
            <sysproperty key='test.targetport' value='${test.targetport}'/>
            <!-- <test name='com.admc.jamama.smtp.SMTPTest'/> Single test -->
            <batchtest>
                <fileset dir='testclasses' includes='**/*Test.class'/>
            </batchtest>
        </junit>
    </target>
    

My version always tells me the summary whether I specify printsummary or not (contrary to the API spec). Do try different combinations of formatters and showoutout, because the output from the Ant target is very different from the standalone Test Runners.