3

我知道解决方案是以某种方式确保在 hamcrest 之后加载 Junit。我有一个 intellij 项目,我在其中设置了一个外部库,其中包含 JUnit 和 JMock 以及 hamcrest。如何确保不会出现此错误

4

3 回答 3

6
  1. junit:junit:4.11 替换了以前的 junit:junit-dep
  2. JUnit 4.11 包含 org.hamcrest:hamcrest-core:1.3 jar 作为依赖项
  3. 要在测试中使用显式 Hamcrest 匹配器,请包含 org.hamcrest:hamcrest-library:1.3。
  4. org.mockito:mockito-all:1.9.x - 包括旧版本的 Hamcrest,不是作为依赖项,而是包装在其 jar 中。这是罪魁祸首。(https://code.google.com/p/mockito/issues/detail?id=459
  5. 将 org.hamcrest:hamcrest-core:1.3 放在所有测试依赖项之前
  6. 使用 Maven 的 dependencyManagement 部分来控制 junit、hamcrest 和 mockito jar 的所有传递版本。例如 spring-integration-test 传递地包括 mockito-all:1.9.0。
于 2013-10-30T16:52:09.807 回答
2

您应该确保库(jar)的兼容性。如果 jar 中的一个类使用另一个 jar 中的类中的某个方法,并且这个使用的方法是新添加的,并且您使用的是旧 jar,那么您肯定会得到java.lang.NoSuchMethodError.

于 2012-08-22T10:02:37.087 回答
0

您需要导入 junit-dep.jar(其中不包括 hamcrest 依赖项!?),而不是 junit.jar。如果您在一个大型项目中使用 Maven,这可能需要大量追逐以清除所有晦涩的 hamcrest 和 junit 导入。

即将发布的 junit 与 hamcrest 1.3 兼容可能会有所帮助

于 2012-08-26T15:20:28.460 回答