Another instance of the NoSuchMethodError
for the JUnit & Hamcrest combination. Offending code:
另一个用于JUnit和Hamcrest组合的nosuchmethderror实例。违法代码:
assertThat(dirReader.document(0).getFields(), hasItem(
new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") {
@Override
protected String featureValueOf(IndexableField actual) {
return actual.name();
} } ));
Commented lines 152–157 in IndexerTest.java (commit ac72ce)
在IndexerTest中注释行152-157。java(提交ac72ce)
Causes a NoSuchMethodError (see http://db.tt/qkkkTE78 for complete output):
导致nosuch有条不紊(请参见http://db)。tt / qkkkTE78完整的输出):
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.FeatureMatcher.matchesSafely(FeatureMatcher.java:43)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:25)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.junit.Assert.assertThat(Assert.java:770)
at org.junit.Assert.assertThat(Assert.java:736)
at indexer.IndexerTest.testIndexContainsField(IndexerTest.java:152)
The setup:
- JUnit 4.11
- JUnit 4.11
- Hamcrest 1.3
- Hamcrest 1.3
- Using Maven's surefire plugin (version 2.14), which uses its JUnitCoreProvider
- 使用Maven的surefire插件(2.14版),它使用的是JUnitCoreProvider
- Java 7 (OpenJDK)
- Java 7(OpenJDK)
- See pom (commit ac72ce)
- 看到pom(提交ac72ce)
Background:
A NoSuchMethodError
is caused by (compiled) classes that call non existing methods. The specific case of describeMismatch
and the JUnit + Hamcrest combination is often caused by an incompatibility between Hamcrest classes included in JUnit and versions of those classes in the Hamcrest library.
NoSuchMethodError是由调用非现有方法的(编译的)类引起的。描述匹配和JUnit + Hamcrest组合的具体情况通常是由JUnit中包含的Hamcrest类和Hamcrest库中这些类的版本之间的不兼容性造成的。
Attempts to solve the NoSuchMethodError:
-
The pom contains an explicit dependency on Hamcrest-library 1.3, Hamcrest-core 1.3, and JUnit 4.11, (in that order) as suggested by Garrett Hall in answer to Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismatch" when running test in IntelliJ 10.5
pom包含对Hamcrest-library 1.3、Hamcrest-core 1.3和JUnit 4.11(按此顺序)的明确依赖,这是Garrett Hall在IntelliJ 10.5运行测试时建议的
-
According to the JUnit documentation the JUnit 4.11 Maven dependency does no longer include compiled Hamcrest classes, instead it has a dependency on Hamcrest-core 1.3; so the
NoSuchMethodError
should not occur.根据JUnit文档,JUnit 4.11 Maven依赖项不再包含已编译的Hamcrest类,而是依赖于Hamcrest-core 1.3;所以不应该发生NoSuchMethodError。
-
Checking the dependency tree with
mvn dependency:tree
as suggested by Dan in answer to junit and hamcrest declaration shows the explicit dependencies on Hamcrest 1.3 and JUnit 4.11 and no other dependencies to those files (see http://db.tt/C2OfTDJB for the complete output).使用mvn依赖项检查依赖树:根据junit和hamcrest声明,Dan建议的树显示了对hamcrest 1.3和junit 4.11的显式依赖,以及对这些文件没有其他依赖项(参见http://db)。tt/C2OfTDJB用于完成输出)。
-
In another test the
NoSuchMethodError
was avoided by using:在另一项测试中,nosuchmethderror避免使用:
assertThat( "Zylab detector not available", d.getDetectors(), hasItem(Matchers.<Detector>instanceOf(ZylabMetadataXmlDetector.class)));
In lines 120–123 of IndexerTest.java (commit ac72ce) instead of the more obvious:
在IndexerTest 120-123行。java(提交ac72ce)而不是更明显的:
assertThat( "Zylab detector not available", d.getDetectors(), hasItem(isA(ZylabMetadataDetector.class));
I'm uncertain whether the explicit type parameter
<Detector>
, usinginstanceOf
instead ofisA
, the explicit reference to Hamcrest'sMatchers
, or a combination of those avoided theNoSuchMethodException
; after fiddling around and trying different things it worked.我不确定显式类型参数 <检测器> ,使用instanceOf代替isA,显式引用Hamcrest's Matchers,还是使用避免nosuchmethdexception的组合;在摆弄和尝试不同的东西后,它起了作用。
-
Using explicit type parameters did not solve/avoid the error.
使用显式类型参数不能解决/避免错误。
-
Using a class derived from
BaseMatcher
instead ofFeatureMatcher
did not solve/avoid the error.使用一个由BaseMatcher派生的类而不是特性匹配器不能解决/避免错误。
Ideas how fix the NoSuchMethodError
?
8 个解决方案
#1
22
This blog helped fix the same problem for me:
这个博客帮助我解决了同样的问题:
https://tedvinke.wordpress.com/2013/12/17/mixing-junit-hamcrest-and-mockito-explaining-nosuchmethoderror/
Inside the dependencies for Mockito and Junit, the author added excludes:
在Mockito和Junit的依赖项中,作者添加了如下内容:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
#2
2
Perhaps one of those other JARs has older versions of Hamcrest's Matcher
or BaseMatcher
. Here's a list of JARs which include the latter, though I have no idea how comprehensive that site is. Is there a Maven plugin which will show you all the dependencies that include a class similar to the dependency tree?
也许那些罐子中的一个有Hamcrest's Matcher或BaseMatcher的旧版。这里有一个jar列表,包括后者,尽管我不知道这个站点有多全面。是否有一个Maven插件将向您显示包含与依赖树类似的类的所有依赖项?
#3
2
If you are using Eclipse, the "Open Type" tool (CTRL+SHIFT+T) can help you find the problematic package. Just search for the class name (e.g., Description), multiple occurrences of the same class from different JARs are red flags.
如果您正在使用Eclipse,“Open Type”工具(CTRL+SHIFT+T)可以帮助您找到有问题的包。只要搜索类名(例如,Description),来自不同jar的同一个类的多次出现就是危险信号。
#4
2
What worked for me was to reorder dependencies. Instead of going mockito, junit, I had to put junit, mockito.
对我起作用的是重新排序依赖项。我没有使用mockito、junit,而是使用junit、mockito。
Mockito 1.9.5 uses hamcrest 1.1 which is incompatible and causes problems.
Mockito 1.9.5使用了hamcrest 1.1,它是不兼容并导致问题的。
#5
2
Using David's tip and How do I split a string on a delimiter in Bash? resulted in the following bash script:
使用David的提示,如何在Bash中分割一个分隔符上的字符串?导致了以下bash脚本:
( IFS=":"; for i in `mvn dependency:build-classpath | grep -v '\[INFO\]'`; do jar tf $i | awk "{print \"$i\\t\" \$1}"; done | grep Matcher )
(online at http://www.kaspervandenberg.net/2013/scripts/findDependencyClass.sh)
在http://www.kaspervandenberg.net/2013/scripts/findDependencyClass.sh(在线)
Which found that dependency JGlobus-Core-2.0.4
has its own versions of org.hamcrest.BaseMatcher
, org.hamcrest.CoreMatchers
, and org.hamcrest.Matcher
.
发现依赖项JGlobus-Core-2.0.4有它自己的org.hamcrest版本。BaseMatcher org.hamcrest。CoreMatchers,org.hamcrest.Matcher。
#6
0
If you are using Eclipse: For me, in eclipse-> project properties->Java build Path moving mockito-all-1.9.5.jar to the bottom of the 'Order and Export' list did the trick. Just above that I have junit-4.11.jar and above that hamcrest-core-1.3.jar
如果您正在使用Eclipse:对我来说,在Eclipse ->项目属性中—>Java构建路径移动mockito-all-1.9.5。jar到“订单和导出”列表的底部就是这样做的。再往上就是6月4日了。在那个仓鼠-核心-1。3。jar上面
#7
0
I solved this jar hell
problem in my Gradle
project with the code below:
我用下面的代码解决了这个jar hell的问题:
testCompile (group: 'junit', name: 'junit', version: '4+') {
exclude group: 'org.hamcrest'
}
testCompile ('org.mockito:mockito-core:1+') {
exclude group: 'org.hamcrest'
}
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
#8
0
For a project with Gradle
as a build tool:
对于以Gradle为构建工具的项目:
testCompile("junit:junit:4.11") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'org.hamcrest', module: 'hamcrest-library'
}
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
#1
22
This blog helped fix the same problem for me:
这个博客帮助我解决了同样的问题:
https://tedvinke.wordpress.com/2013/12/17/mixing-junit-hamcrest-and-mockito-explaining-nosuchmethoderror/
Inside the dependencies for Mockito and Junit, the author added excludes:
在Mockito和Junit的依赖项中,作者添加了如下内容:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
#2
2
Perhaps one of those other JARs has older versions of Hamcrest's Matcher
or BaseMatcher
. Here's a list of JARs which include the latter, though I have no idea how comprehensive that site is. Is there a Maven plugin which will show you all the dependencies that include a class similar to the dependency tree?
也许那些罐子中的一个有Hamcrest's Matcher或BaseMatcher的旧版。这里有一个jar列表,包括后者,尽管我不知道这个站点有多全面。是否有一个Maven插件将向您显示包含与依赖树类似的类的所有依赖项?
#3
2
If you are using Eclipse, the "Open Type" tool (CTRL+SHIFT+T) can help you find the problematic package. Just search for the class name (e.g., Description), multiple occurrences of the same class from different JARs are red flags.
如果您正在使用Eclipse,“Open Type”工具(CTRL+SHIFT+T)可以帮助您找到有问题的包。只要搜索类名(例如,Description),来自不同jar的同一个类的多次出现就是危险信号。
#4
2
What worked for me was to reorder dependencies. Instead of going mockito, junit, I had to put junit, mockito.
对我起作用的是重新排序依赖项。我没有使用mockito、junit,而是使用junit、mockito。
Mockito 1.9.5 uses hamcrest 1.1 which is incompatible and causes problems.
Mockito 1.9.5使用了hamcrest 1.1,它是不兼容并导致问题的。
#5
2
Using David's tip and How do I split a string on a delimiter in Bash? resulted in the following bash script:
使用David的提示,如何在Bash中分割一个分隔符上的字符串?导致了以下bash脚本:
( IFS=":"; for i in `mvn dependency:build-classpath | grep -v '\[INFO\]'`; do jar tf $i | awk "{print \"$i\\t\" \$1}"; done | grep Matcher )
(online at http://www.kaspervandenberg.net/2013/scripts/findDependencyClass.sh)
在http://www.kaspervandenberg.net/2013/scripts/findDependencyClass.sh(在线)
Which found that dependency JGlobus-Core-2.0.4
has its own versions of org.hamcrest.BaseMatcher
, org.hamcrest.CoreMatchers
, and org.hamcrest.Matcher
.
发现依赖项JGlobus-Core-2.0.4有它自己的org.hamcrest版本。BaseMatcher org.hamcrest。CoreMatchers,org.hamcrest.Matcher。
#6
0
If you are using Eclipse: For me, in eclipse-> project properties->Java build Path moving mockito-all-1.9.5.jar to the bottom of the 'Order and Export' list did the trick. Just above that I have junit-4.11.jar and above that hamcrest-core-1.3.jar
如果您正在使用Eclipse:对我来说,在Eclipse ->项目属性中—>Java构建路径移动mockito-all-1.9.5。jar到“订单和导出”列表的底部就是这样做的。再往上就是6月4日了。在那个仓鼠-核心-1。3。jar上面
#7
0
I solved this jar hell
problem in my Gradle
project with the code below:
我用下面的代码解决了这个jar hell的问题:
testCompile (group: 'junit', name: 'junit', version: '4+') {
exclude group: 'org.hamcrest'
}
testCompile ('org.mockito:mockito-core:1+') {
exclude group: 'org.hamcrest'
}
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
#8
0
For a project with Gradle
as a build tool:
对于以Gradle为构建工具的项目:
testCompile("junit:junit:4.11") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'org.hamcrest', module: 'hamcrest-library'
}
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'