在运行测试之前,Ant JUnit任务失败

时间:2016-03-30 20:52:07

标签: ant junit junit4

我有以下蚂蚁目标:

<path id="test-classpath">
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
    <pathelement path="bin/release" />
</path>

<target name="run-test">
    <junit printsummary="yes" haltonfailure="yes" fork="true">
        <formatter type="failure"/>
        <test name="someNamespace.someTest" />
        <classpath refid="test-classpath" />
    </junit>
</target>

当我尝试从命令行运行它时,我得到以下输出:

>ant run-test
Buildfile: ...\build.xml
     [echo] 2016-03-30 20:29:29

run-test:
    [junit] Exception in thread "main" java.lang.reflect.InvocationTargetException
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.FormatterElement.createFormatter(FormatterElement.java:343)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.FormatterElement.createFormatter(FormatterElement.java:257)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.transferFormatters(JUnitTestRunner.java:1085)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1180)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
    [junit] Caused by: java.lang.reflect.InvocationTargetException
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    [junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [junit]     at java.lang.reflect.Method.invoke(Method.java:498)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.FormatterElement.createFormatter(FormatterElement.java:337)
    [junit]     ... 4 more
    [junit] Caused by: java.lang.NullPointerException
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.FailureRecorder.setProject(FailureRecorder.java:153)
    [junit]     ... 9 more

BUILD FAILED
...\build.xml:171: Using loader AntClassLoader[
    C:\Program Files (x86)\apache-ant-1.9.6\lib\ant-launcher.jar;
    C:\Program Files (x86)\apache-ant-1.9.6\lib\ant.jar;
    C:\Program Files (x86)\apache-ant-1.9.6\lib\ant-junit.jar;
    C:\Program Files (x86)\apache-ant-1.9.6\lib\ant-junit4.jar;
    ...\lib\commons-math3-3.5.jar;
    ...\lib\hamcrest-core-1.3.jar;
    ...\lib\junit-4.11.jar;
    ...\lib\log4j-api-2.5.jar;
    ...\lib\log4j-core-2.5.jar;
    ...\bin\release] on class org.apache.tools.ant.taskdefs.optional.junit.FailureRecorder:
java.lang.NoClassDefFoundError: junit/framework/TestListener
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.apache.tools.ant.AntClassLoader.findBaseClass(AntClassLoader.java:1407)
        at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1085)
        at org.apache.tools.ant.util.SplitClassLoader.loadClass(SplitClassLoader.java:58)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.apache.tools.ant.taskdefs.optional.junit.FormatterElement.createFormatter(FormatterElement.java:287)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.logVmExit(JUnitTask.java:1854)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.logVmCrash(JUnitTask.java:1818)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1295)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:1024)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:2105)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:832)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:435)
        at org.apache.tools.ant.Target.performTasks(Target.java:456)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
        at org.apache.tools.ant.Main.runBuild(Main.java:853)
        at org.apache.tools.ant.Main.startAnt(Main.java:235)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)
Caused by: java.lang.ClassNotFoundException: junit.framework.TestListener
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 51 more

Total time: 3 seconds

测试在Eclipse Mars(4.5.2)中成功运行。有谁知道为什么会发生这种情况,以及如何让我的JUnit测试通过ant运行?我正在使用JUnit 4.11,并尝试过ant v1.9.3和v1.9.6(上面的输出是从ant v1.9.6生成的)。

修改

我还根据评论中的建议使用ant v1.8.4进行了测试,但这对结果崩溃没有任何影响(除了jar文件的版本号)。

2 个答案:

答案 0 :(得分:0)

根据这个问题 - Class not found with Ant, Ivy and JUnit - error in build.xml? - ant 1.9+中存在一个错误,你应该尝试使用ant 1.8.4。

答案 1 :(得分:0)

我终于弄明白了问题所在。我的JUnit任务类路径不正确,因为我忘了包含我的测试套件类的位置。一旦我纠正了类路径,一切都按预期工作。不过,我觉得这是一个应该由Ant或JUnit团队解决的bug。诸如“无法加载XXX类”之类的消息会更有帮助。