The JUnit test fails with an Ant error with the task `<junit>`, but passes with `<exec>`?
I am automating my JUnit tests in my Ant build. However, my simple test passes only when launched from the IDE and command line, but does not run with the Ant <junit> task. When I run it from the command line (technically I use the Ant <exec> task), the result:
clean: compile_tests: [javac] Compiling 2 source files to C:\MY_TEMP junit_exec: [exec] JUnit version 4.10 [exec] . [exec] Time: 0.004 [exec] [exec] OK (1 test) [exec] BUILD SUCCESSFUL Total time: 1 second but when I use the <junit> task:
Buildfile: C:\MY_TEMP\build.xml clean: compile_tests: [javac] Compiling 2 source files to C:\MY_TEMP junit_ant: [echo] junit_ant started [junit] Test SimpleTest FAILED BUILD SUCCESSFUL Total time: 0 seconds MY_TEMP content: junit-4.10.jar , SimpleTest.java and build.xml .
I copied junit-4.10.jar to the %ANT_HOME%\lib folder as an Ant junit task documentation suggestion. He already had both ant-junit.jar and ant-junit4.jar .
My version of Java is 1.6.0_26.
My test:
// YES, this is the default package import org.junit.*; public class SimpleTest { @Test public void mySimpleTest(){ Assert.assertEquals( 2, 1 + 1 ); } } And my Ant file (build.xml):
<?xml version="1.0"?> <project name="regression_tests" basedir="."> <target name="clean"> <delete> <fileset dir="." includes="*.class" /> </delete> </target> <target name="compile_tests" depends="clean"> <javac srcdir="." destdir="." source="1.6" target="1.6" includeantruntime="false" > <classpath> <pathelement location="./junit-4.10.jar" /> </classpath> </javac> </target> <target name="junit_ant" depends="compile_tests" > <echo message="junit_ant started" /> <junit> <test name="SimpleTest" /> </junit> </target> <target name="junit_exec" depends="compile_tests"> <exec executable="java" dir="." > <arg value="-classpath" /> <arg value=".;junit-4.10.jar" /> <arg value="org.junit.runner.JUnitCore" /> <arg value="SimpleTest" /> </exec> </target> </project> In particular, I edited the junit_ant task:
<junit> <classpath location="." /> <test name="SimpleTest" /> <formatter type="xml" /> </junit> <junitreport todir="."> <fileset dir="."> <include name="TEST-*.xml" /> </fileset> <report todir="." /> </junitreport> Which then showed me that the failure was java.lang.ClassNotFoundException: SimpleTest , so I just added <classpath location="." /> <classpath location="." /> to the <junit> task, and then it worked.