Programming Tips - ant: junit from Ant

Date: 2014oct28 Language: xml Product: junit, ant Q. ant: junit from Ant A. The first thing to know is that it runs on compiled code. So don't point it at your .java files. It expects your compiled code to be put into a .jar file. Note that you can not use a .war file -- it must be a regular .jar file. So for me the first thing was a target to make the .jar file:
<target name="jar" depends="compile"> <mkdir dir="${test}"/> <jar jarfile="${test}/myproject.jar" basedir="${build}"/> </target>
Next, I have classes called * that I want to run junit on. These are compiled into *Test.class in the build folder. We make this target depend the "jar" step.
<target name="test" depends="jar"> <junit printsummary="on" haltonfailure="yes" showoutput="yes" logfailedtests="yes"> <!-- Needed so errors are displayed --> <formatter type="plain" usefile="false"/> <-- If you have a master-classpath add it to the classpath --> <classpath refid="master-classpath"/> <-- Now, add the .jar we just built --> <classpath> <pathelement location="${test}/myproject.jar"/> </classpath> <batchtest> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> </target>
Finally this can be run via:
ant test