禁止导入 JUnit 3 类

Disable import of JUnit 3 classes

我只想开发 JUnit 4 测试。在编写单元测试时,Eclipse 经常从 junit.framework 导入 类,即 JUnit 3。

这导致了各种问题,例如当期望异常时,如果它在错误的包中,它根本不会捕获它,如下所示:

import static org.junit.Assert.*;
import junit.framework.ComparisonFailure;
[...]
try 
{
     assertEquals(0, 1);
}
catch(ComparisonFailure cfe)
{
}

很奇怪,如果我 Ctrl+ComparisonFailure 上单击 ,它会显示

Source not found

The JAR of this class belongs to container 'JUnit 4' [...]

可能有用的环境信息:

如何阻止 Eclipse 导入 JUnit 3 类?

我读过 Why is Eclipse using JUnit 3 when I have junit-4.3.1.jar in my build path?,但它太旧了,可能不再适用于 Luna。另外,我的问题不在 运行 测试中,而是在 实现 测试中。

你的插件有哪些依赖项? Junit 3.X 或 4.X ?

您可以在您的工作区中搜索 junit 3 中的任何引用和 change/remove 它们。

实际上,JUnit 4 依赖于一些 class 最初在 JUnit 3 中开发或驻留在包 junit.* 中的元素。其中之一 class 是 ComparisonFailure。如果您查看最新的 JUnit 4.12,您会发现这些包仍然存在。

但是,sources jar 确实包含这些 classes 的 java 文件。也许您包含 JUnit 的库(您使用 Eclipse JUnit 库吗?)缺少这些的源文件?你的依赖(junit.jar)来自哪里?

Eclipse 用户的另一种解决方法是以下解决方案: Windows -> 首选项 -> Java -> 外观 -> 类型过滤器 并将 junit.framework.* 添加到排除列表中。