与 Ant 集成的 Junit 测试因 ClassNotFoundException 失败
我对我的项目进行了 JUnit 测试,可以使用 Eclipse 正确运行。
所以,现在我尝试将这些测试与 ant 任务集成。为此,我编写了以下 ant 脚本:
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath refid="classpath-test" />
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>
目录 ${libtest.hom} 包含 junit-4.8.1.jar 和 hamcrest-core-1.1.jar。
当我启动以下命令:ant unit-test 时,MyTest 的执行失败并显示以下输出:
unit-test:
[junit] Testsuite: com.test.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] com.test.MyTest
[junit] java.lang.ClassNotFoundException: com.test.MyTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
这很奇怪,因为 com.test.MyTest 位于 ant 脚本中指向我的任务 junit 的类路径中。有人会有解决这个问题的想法吗?
感谢您的帮助。
西尔万.
I have JUnit tests on my project that run correctly with Eclipse.
So, now I try to integrate these tests with an ant task. To make that I make the following ant script :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath refid="classpath-test" />
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>
The directory ${libtest.hom} contains junit-4.8.1.jar and hamcrest-core-1.1.jar.
When I launch the following command : ant unit-test, the execution of the MyTest fails with the following output :
unit-test:
[junit] Testsuite: com.test.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] com.test.MyTest
[junit] java.lang.ClassNotFoundException: com.test.MyTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
It's very strange because com.test.MyTest is well in the classpath pointed for my task junit in ant script. Someone would have and idea to solve this problem ?
Thanks for your help.
Sylvain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
${testclasses.home}
目录不在
任务的类路径上。我认为这是 com.test.MyTest 的类文件所在的位置。
这是修改后的单元测试目标:
The
${testclasses.home}
directory is nowhere on a classpath for<junit>
task.I think this is where class file for
com.test.MyTest
lives.Here is modified unit-test target: