【问题标题】:Ant JUnit Task couldn't find junit/framework/TestCase.classAnt JUnit Task 找不到 junit/framework/TestCase.class
【发布时间】:2011-05-16 03:50:11
【问题描述】:

我正在尝试让 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 中运行良好。

提前致谢!

【问题讨论】:

    标签: eclipse ant junit classpath


    【解决方案1】:

    该错误是由于您的测试类无法加载造成的。

    您是否将包 mypackage.tests.controller(即包含类文件或 jar 存档的文件夹)放在 junit 调用的类路径中?这可以使用嵌套的类路径结构来完成。 (http://ant.apache.org/manual/Tasks/junit.html)。

    【讨论】:

    • 天啊,我会哭的。两天的时间!?这正是问题所在。我不知道如何只添加类文件,所以我包含了一个 任务并将 jar 文件添加到类路径中。
    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多