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

眉间皱痕 提交于 2019-12-12 13:05:23

问题


I understand that the solution is to somehow make sure that Junit is loaded after hamcrest. I have an intellij project, in which I setup an external library, which contains both JUnit and JMock and hamcrest. How can I make sure that this error does not show up


回答1:


You should make sure the compatibility of libraries(jars). If a class inside jar uses some method from class which is in another jar, and this used method is newly added and you are using old jar then definitely you will get java.lang.NoSuchMethodError.




回答2:


  1. junit:junit:4.11 replaces the former junit:junit-dep
  2. JUnit 4.11 includes the org.hamcrest:hamcrest-core:1.3 jar as a dependency
  3. To use explicit Hamcrest matchers in tests, include org.hamcrest:hamcrest-library:1.3.
  4. org.mockito:mockito-all:1.9.x - includes an OLD version of Hamcrest, not as a dependency but wrapped within its jar. THIS is the culprit. (https://code.google.com/p/mockito/issues/detail?id=459)
  5. Put org.hamcrest:hamcrest-core:1.3 before all test dependencies
  6. Use Maven's dependencyManagement section to control all transitive versions of junit, hamcrest and mockito jars. For example spring-integration-test transitively includes mockito-all:1.9.0.



回答3:


You need to import junit-dep.jar (which doesn't include the hamcrest dependency!?), rather than junit.jar. If you're using Maven on a large project, this can involve a lot of chasing to flush out all the obscure hamcrest and junit imports.

It might help that the upcoming release of junit will be compatible with hamcrest 1.3



来源:https://stackoverflow.com/questions/12070514/jmock-java-lang-nosuchmethoderror-org-hamcrest-matcher-describemismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!