I am automating my JUnit tests into my Ant build. However, my simple test only passes when run from the IDE and commandline but fails with Ant's <junit>
task. When I run it from the commandline (I am technically using the Ant <exec>
task) the result is:
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
The contents of MY_TEMP
are junit-4.10.jar
, SimpleTest.java
, and build.xml
.
I have copied junit-4.10.jar
to the %ANT_HOME%\lib
folder as suggest by the Ant junit task documentation. It already had both ant-junit.jar
and ant-junit4.jar
.
My version of Java is 1.6.0_26.
My test is:
// 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 ), is:
<?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>
If a test passes one way, and fails another, it's likely something classpath-related, like it can't find a test class, a class under test, or a library.
The test output should help clarify if this is what the issue is.