Unit test fails only when running from with Ant ta

2019-07-31 15:23发布

I run the same tests from both eclipse and from an Ant task. When running from eclipse all tests pass. When I run Ant junit task, one, single test fails with the following strange error:

junit.framework.AssertionFailedError at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137) at unitests.mypackage.MyTestClass.myTestCase(Unknown Source)

What can be the cause?

I read a little and found out that it might be because eclipse and Ant use different versions of junit. In my project, junit is located at libs/junit-4.10.jar and referenced both in eclipse's .classpath file and in junit task classpath. You can see Ant's task here:

<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>

The version of Ant is 1.7.1 and it came with eclipse.

Edit:

Eventually solved it by adding fork="yes" to junit's task. Found it by generating build file using eclipse's export option, and then looking at the differences between the generated file and mine. No idea why forking solves the problem though.

2条回答
甜甜的少女心
2楼-- · 2019-07-31 16:12

As the stack trace shows org.eclipse entries, I strongly suspect you are running ant via Eclipse. If you run ant from the console you will very likely not have this problem.

The cause, therefore, is a bug in Eclipse. This should be no surprise, Eclipse is full of bugs.

fork="true" helps because it causes ant to spawn a new process for execution. This new process does not have any Eclipse classes on the classpath.

查看更多
虎瘦雄心在
3楼-- · 2019-07-31 16:17

Different versions of Ant sound like a likely cause. To test that, set which Ant version you want Eclipse to use.

It's probably also a good idea to set the same version of JUnit and the same JDK that you use to run the test outside the IDE.

查看更多
登录 后发表回答