Ant JUnit任务找不到junit/framework/TestCase.class
我正在尝试让 JUnit 4.8.1 任务在 Ant 1.7.1 中运行。我的 IDE 是 Eclipse Helios。我已经用头撞砖墙两天了,无法弄清楚这一点。通过阅读其他帖子,我确信这是一个类路径问题,但我看不出哪里出了问题。
我的 JUnit4 测试套件定义如下:
package mypackage.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* JUnit 4 Test Suite for the entire <code>mypackage</code>
* package
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ mypackage.tests.controller.AllTests.class })
public class AllTests {
}
...足够简单,但测试失败并出现 ClassNotFoundException
java.lang.ClassNotFoundException: mypackage.tests.AllTests
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
当我打开调试标志时,我从 Ant 得到以下跟踪:
(...)
[junit] Couldn't find junit/framework/TestCase.class
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
fileset: Setup scanner in dir C:\eclipse\plugins with patternSet{ includes: [org.junit_4.8.1.v4_8_1_v20100427-1100/junit.jar] excludes: [] }
Finding class junit.framework.Test
Loaded from C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar junit/framework/Test.class
(...)
显然,JUnit jar 位于类路径上,而其他类例如 Test.class 正在被拾取,那么为什么我会收到“无法找到 TestClass.class”消息?
我尝试将 JUnit.jar 添加到 Eclipse GUI 中的 ant 类路径中,这具有以下效果:
(...)
[junit] Found C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
(...)
[junit] WARNING: multiple versions of ant detected in path for junit
[junit] jar:file:C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit] and jar:file:/C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
(...)
有人可以请帮我让它工作吗!
哦,我差点忘了...测试套件在 Eclipse 中运行良好。
提前致谢!
I'm trying to get a JUnit 4.8.1 task to run in Ant 1.7.1. My IDE is Eclipse Helios. I've been banging my head against a brick wall for 2 days now and cannot figure this out. I'm sure from reading other posts its a classpath problem, but I can't see where I'm going wrong.
My JUnit4 test suite is defined as follows:
package mypackage.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* JUnit 4 Test Suite for the entire <code>mypackage</code>
* package
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ mypackage.tests.controller.AllTests.class })
public class AllTests {
}
...simple enough, but the test fails with a ClassNotFoundException
java.lang.ClassNotFoundException: mypackage.tests.AllTests
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
When I switch on the debug flag, I get the following trace from Ant:
(...)
[junit] Couldn't find junit/framework/TestCase.class
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
fileset: Setup scanner in dir C:\eclipse\plugins with patternSet{ includes: [org.junit_4.8.1.v4_8_1_v20100427-1100/junit.jar] excludes: [] }
Finding class junit.framework.Test
Loaded from C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar junit/framework/Test.class
(...)
Clearly the JUnit jar is on the classpath, and other classes such as Test.class are being picked up, so why am I getting the 'couldn't find TestClass.class' message?
I tried adding the JUnit.jar to the ant classpath in the Eclipse GUI, and this has the following effect:
(...)
[junit] Found C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
(...)
[junit] WARNING: multiple versions of ant detected in path for junit
[junit] jar:file:C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit] and jar:file:/C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
(...)
Can someone please please please help me to get this working!
Oh, I almost forgot... the test suite runs fine from within Eclipse.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是由于您的测试类无法加载而引起的。
您是否将包 mypackage.tests.controller (即包含类文件或 jar 存档的文件夹)放在 junit 调用的类路径上?这可以使用嵌套类路径结构来完成。 (http://ant.apache.org/manual/Tasks/junit.html)。
The error is caused by the fact that your test classes cannot be loaded.
Did you put the package mypackage.tests.controller (i.e. the folder(s) containing the class files or the jar archive) on the classpath of your junit call? This can be done using a nested classpath structure. (http://ant.apache.org/manual/Tasks/junit.html).