Chapter 5. Testing Builds with JUnit

This chapter is about a crucial aspect of the build processtesting build results before deploying them. It doesn't make sense to deploy a build that has been broken, and using the JUnit framework with Ant, you can run tests on your code and deploy a build only if it satisfies those tests. This is a great way to make sure changes to your code haven't broken anything.

To test the results of a build automatically, you'll need to use one of Ant's most powerful optional tasks: junit. This task is part of the repertoire of every serious Ant developer, especially those working in teams. If someone else on your project has broken your code, you should know about it before you deploy or upload to a shared code repository, and junit will let you know about these problems automatically.

To demonstrate how JUnit works with Ant in this chapter, we're going to use, shown in Example 5-1, as a guinea pig.

Example 5-1. A simple Project file
package org.antbook; public class Project  {     public Project (String name)      {     }          public boolean returnTrue( )      {         return true;     }     public int return4( )      {         return 2 + 2;     }     public Object returnObject( )      {         return new Integer(1);     }     public static void main(String args[])      {         Project project = new Project("project");         System.out.println(project.returnTrue( ));         System.out.println(project.return4( ));         System.out.println(project.returnObject( ));     } }

This application,, has three simple methods, each of which returns a value:

returnTrue( )

returns a boolean value of TRue.

return4( )

returns an integer value of 4.

returnObject( )

returns an Integer object containing a value of 1.

When you compile and run it, each of these methods are executed. Now you can use the junit task to make sure that alterations to this application's code doesn't break the expected operation of these methods.

You can see the original build file that builds and deploys the application in Example 5-2: Note the section where the JUnit tests will be added. Break the build if those tests don't pass.

Example 5-2. Using Junit ch05/junit/build.xml
<?xml version="1.0" ?> <project default="main">     <property name="message" value="Building the project...." />     <property name="testsOK" value="Tested OK...." />     <property name="src" location="source" />     <property name="output" location="." />     <property name="results" location="results" />     <property name="jars" location="jars" />     <property name="dist" location="user" />     <property name="junit.fork" value="true"/>     <target name="main" depends="init, compile, test, compress, deploy">         <echo>           ${message}         </echo>     </target>        <target name="init">         <mkdir dir="${output}" />         <mkdir dir="${results}" />         <mkdir dir="${jars}" />     </target>        <target name="compile">         <javac srcdir="${src}" destdir="${output}" />     </target>     <target name="test" depends="test1, test2, test3, test4, test5">         <echo>           ${testsOK}         </echo>     </target>         .         .         .     <!-- [TESTS GO HERE] -->         .         .         .     <target name="compress">         <jar destfile="${jars}/Project.jar" basedir="${output}">           <include name="**/*.class"/>         </jar>     </target>     <target name="deploy">         <delete dir="${dist}" />         <mkdir dir="${dist}" />         <copy todir="${dist}">           <fileset dir="${jars}">               <include name="*.jar"/>           </fileset>         </copy>     </target> </project>

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: