JMock- java.lang.NoSuchMethodError:org.hamcrest.Matcher.describeMismatch()

时间:2012-08-22 09:51:19

标签: java jmock hamcrest

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

3 个答案:

答案 0 :(得分:5)

  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 transitively包括mockito-all:1.9.0。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

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

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