Ant JUnit抛出ClassNotFoundException

时间:2017-05-04 15:55:15

标签: java junit ant travis-ci

我正在IntelliJ IDE中开发Java应用程序。为了能够将我的repo连接到Travis CI,我使用IDE生成了ant build.xml。但是,IntelliJ尚未在构建文件中创建测试目标。我通过添加以下内容手动编辑它:

<target name="test" depends="compile.module.pearplanner.tests">
    <junit>
        <classpath>
            <pathelement location="lib/junit-4.12.jar"/>
        </classpath>
        <batchtest>
            <fileset dir="./Test/">
                <include name="**/*Test*" />
            </fileset>
        </batchtest>
        <formatter type="brief" usefile="false"/>
    </junit>
</target>

当我跑步时

ant test 

我收到以下错误:

test:
[junit] Testsuite: Model.AccountTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] 
[junit] Null Test:  Caused an ERROR
[junit] Model.AccountTest
[junit] java.lang.ClassNotFoundException: Model.AccountTest
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:348)
[junit] 
[junit] 
[junit] Test Model.AccountTest FAILED
[junit] Testsuite: Model.PersonTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] 
[junit] Null Test:  Caused an ERROR
[junit] Model.PersonTest
[junit] java.lang.ClassNotFoundException: Model.PersonTest
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:348)
[junit] 
[junit] 
[junit] Test Model.PersonTest FAILED
BUILD SUCCESSFUL
Total time: 0 seconds

My project structure

我已经测试了我可以在网上找到的多种方法,但所有这些方法都给了我相同的输出。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。原来我必须在类路径中指定类输出目录和测试类输出目录。还有hamcrest-core-1.3.jar也没有。正确的版本:

<target name="test" depends="build.modules" >
    <junit haltonerror="yes" haltonfailure="yes">
        <classpath>
            <pathelement location="${basedir}/lib/junit-4.12.jar"/>
            <pathelement location="${basedir}/lib/hamcrest-core-1.3.jar"/>
            <pathelement location="${pearplanner.output.dir}/"/>
            <pathelement location="${pearplanner.testoutput.dir}/"/>
        </classpath>
        <batchtest>
            <fileset dir="${pearplanner.testoutput.dir}">
                <include name="**/*Test*" />
            </fileset>
        </batchtest>
        <formatter type="brief" usefile="false"/>
    </junit>
</target>