简体   繁体   中英

JUnit test passes in Eclipse workbench but fails in Ant build

I have a unit test that contains 6 test cases ( @Test -annotated methods) that all run perfectly when I run the Java file as a JUnit Test (from inside Eclipse workbench). But when I go to run a run-tests Ant target from a buildscript, they fail with the following console output:

[junit] Running com.me.myproject.WidgetTest
[junit] Tests run: 6, Failures: 6, Errors: 0, Time elapsed: 1.737 sec
[junit] Test com.me.myproject.WidgetTest FAILED

Here is the JUnit section of the run-tests target:

<junit fork="yes" forkmode="once" dir="${basedir}" printsummary="yes" haltonerror="no" haltonfailure="no">
    <classpath>
        <path refid="test.class.path"/>
        <pathelement location="${mainBuildDir}"/>
        <pathelement location="${testBuildDir}"/>
    </classpath>

    <formatter type="xml"/>

    <batchtest todir="${genUnitTestReportsDir}">
        <fileset dir="${testJavaSrcDir}">
        <include name="**/*Test*.java"/>
        </fileset>
    </batchtest>
</junit>

Anybody ever hear of this happening? Is there any way to get more (verbose) output from JUnit's ant task? Any good way to debug what's happening here? Thanks in advance!

I can't tell without seeing the stacktrace of the exception occuring. It's probably a classpath issue. Check the Java ANT documentation about JUnit, it should help you to enable debug traces.

I have had similar problems with classes that write to databases when using Hibernate/Hypersonic SQL for my tests. The life of the Hypersonic connection when running tests inside eclipse is much shorter than the one used by ant. As a result, the databases are deleted at the end of every test in Eclipse, but stick around from test to test inside ant.

If you write data in your databases, you need to delete them in the tear down to ensure the next test won't pick up your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM