仅在使用Ant任务运行时,单元测试才会失败

时间:2011-12-10 22:17:12

标签: java eclipse ant junit

我从eclipse和Ant任务中运行相同的测试。从eclipse运行时,所有测试都通过。当我运行Ant junit任务时,单个测试失败并出现以下奇怪错误:

  

junit.framework.AssertionFailedError   在org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)   在org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)   在org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)   在unitests.mypackage.MyTestClass.myTestCase(未知来源)

可能是什么原因?

我读了一下,发现可能是因为eclipse和Ant使用不同版本的junit。在我的项目中,junit位于libs / junit-4.10.jar,并在eclipse的.classpath文件和junit task classpath中引用。 你可以在这里看到Ant的任务:

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
    <fileset dir="${src.dir}" includes="**/*.jar"/>
</path>
...
<target name="run-unit-tests" depends="compile,compile-unit-tests">
    <mkdir dir="${junit.output.dir}"/>
    <junit fork="yes" printsummary="yes" haltonfailure="no">          
        <classpath>
            <path refid="classpath"/>
            <fileset dir="${unit.tests.classes.dir}" includes="**/*.class"/>
        </classpath>

        <formatter type="xml"/>
        <batchtest todir="${junit.output.dir}">
            <fileset dir="${unit.tests.dir}">
                <include name="**/*Test*.java"/>
            </fileset>
        </batchtest>
    </junit>

    <mkdir dir="${junit.report.dir}"/>
    <junitreport todir="${junit.report.dir}">
      <fileset dir="${junit.output.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${junit.report.dir}/html"/>
    </junitreport>
</target>

Ant的版本是1.7.1,它带有eclipse。

修改

最终通过向junit的任务添加fork="yes来解决它。通过使用eclipse的导出选项生成构建文件找到它,然后查看生成的文件和我之间的差异。不知道为什么forking解决了问题

2 个答案:

答案 0 :(得分:3)

不同版本的Ant听起来像是一个可能的原因。为了测试它,你希望Eclipse使用set which Ant version

设置相同版本的JUnit和用于在IDE外部运行测试的same JDK可能也是个好主意。

答案 1 :(得分:2)

由于堆栈跟踪显示org.eclipse条目,我强烈怀疑您是通过Eclipse运行ant。如果你从控制台运行ant,你很可能不会遇到这个问题。

因此,原因是Eclipse中的一个错误。这应该不足为奇,Eclipse充满了bug。

fork="true"会有所帮助,因为它会导致ant生成一个新的执行进程。这个新进程在类路径上没有任何Eclipse类。

相关问题