Java/使用 Junit 4 给出编译错误

标签 java junit

我的项目中属于 Junit 的包(在 org.junit, org.junit.experimental.results, org.junit.internal, org.junit.internal.matchers, org.junit.matchers, org.junit.rules 中)出现编译错误。

所有这些类都在我的项目中的junit包下,意思是:Security/src/org.junit, Security/src/org.junit.experimental.results ,其中Security是我的项目的名称。我编写的类位于我定义的其他包下:Security/src/myPackage/myClass

在所有这些类中,它都会在导入行中给出编译错误,例如:

import org.hamcrest.Description cannot be resolved
import org.hamcrest.Matcher cannot be resolved;
import static org.junit.matchers.JUnitMatchers.containsString cannot be resolved;

检查进展顺利(显示绿色/红色条,并给出正确的结果),但是这个问题给我所有项目带来了错误。

最佳答案

您的 JUnit 版本需要 Hamcrest。您应该将其包含在构建路径中或使用旧的 JUnit 版本。

4.10 版本有不同的 JUnit 包:

  • junit-4.10.jar:包括 Hamcrest,您应该使用它。
  • junit-dep-4.10.jar:不包括 Hamcrest,如果您的构建路径中已经有 hamcrest jar,请使用它。

关于Java/使用 Junit 4 给出编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317701/

相关文章:

java - 拦截对引用监视器的方法调用?

Java 和正则表达式 : Matching a substring that is not preceded by specific characters

java - @Before 和 @After 排除

java - 模拟一个返回 Stream 并被多次调用的方法

java - 实现自定义数据结构以替换 2 个 HashMap

java - 需要正则表达式帮助

java - Junit 参数化测试失败,并出现 java.lang.IllegalArgumentException : argument type mismatch

java - 模拟父 final方法

java - 联合测试方法

java - 如何让Android应用程序保持活力