JUnit test fails in Ant with `<junit>` task but passes with `<exec>`?

痴心易碎 提交于 2019-12-24 03:29:54

问题


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>

回答1:


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.




回答2:


Specifically I editted my junit_ant task to:

        <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="." /> to the <junit> task and then it worked.




回答3:


Add this line for more info:

<formatter type="brief" usefile="false"/>


来源:https://stackoverflow.com/questions/7850847/junit-test-fails-in-ant-with-junit-task-but-passes-with-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!