Ant JUnit 抛出 ClassNotFoundException
Ant JUnit throws ClassNotFoundException
我正在使用 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
我测试了可以在网上找到的多种方法,但所有方法都给我相同的输出。我在这里做错了什么?
我设法修复了它。原来我必须在 class 路径中指定 class 输出目录并测试 class 输出目录。 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>
我正在使用 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
我测试了可以在网上找到的多种方法,但所有方法都给我相同的输出。我在这里做错了什么?
我设法修复了它。原来我必须在 class 路径中指定 class 输出目录并测试 class 输出目录。 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>