Maven Hamcrest 1.3和;JUnit4.11
JUnit和Hamcrest组合的Maven Hamcrest 1.3和;JUnit4.11,maven,junit,junit4,maven-surefire-plugin,hamcrest,Maven,Junit,Junit4,Maven Surefire Plugin,Hamcrest,JUnit和Hamcrest组合的NoSuchMethodError的另一个实例。 违规代码: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") { @Override protected String
NoSuchMethodError
的另一个实例。
违规代码:
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();
} } ));
设置:
- JUnit4.11
- 汉克雷斯特1.3
- 使用Maven的surefire插件(版本2.14),该插件使用其JUnitProProvider
- Java 7(OpenJDK)
- 参见(提交)
descripebommatch
和JUnit+Hamcrest组合的具体情况通常是由JUnit中包含的Hamcrest类与Hamcrest库中这些类的版本之间的不兼容引起的
尝试解决NoSuchMethod错误:
- pom包含对Hamcrest library 1.3、Hamcrest core 1.3和JUnit 4.11(按顺序)的显式依赖,如in to所建议的
- 根据JUnit文档,JUnit 4.11 Maven依赖项不再包括已编译的Hamcrest类,而是依赖于Hamcrest core 1.3;因此不应出现
NoSuchMethodError
- 按照in-to的建议,使用
检查依赖关系树,可以显示Hamcrest 1.3和JUnit 4.11上的显式依赖关系,而不显示这些文件的其他依赖关系(完整输出请参阅)mvn-dependency:tree
- 在另一项测试中,通过使用以下方法避免了
:NoSuchMethodError
我不确定是显式类型参数assertThat( "Zylab detector not available", d.getDetectors(), hasItem(Matchers.<Detector>instanceOf(ZylabMetadataXmlDetector.class)));
,使用
而不是instanceOf
,显式引用Hamcrest的isA
,还是这些参数的组合避免了匹配器
;在摆弄和尝试不同的事情之后,它成功了NoSuchMethodException
- 使用显式类型参数无法解决/避免错误
- 使用从
派生的类而不是BaseMatcher
无法解决/避免错误FeatureMatcher
如何修复
NoSuchMethodError
?也许其他的jar中有一个拥有Hamcrest的Matcher
或BaseMatcher
的旧版本。这里有一个包括后者的网站,虽然我不知道这个网站有多全面。是否有一个Maven插件可以向您显示包含类似于依赖关系树的类的所有依赖关系?使用tip并生成以下bash脚本:
( IFS=":"; for i in `mvn dependency:build-classpath | grep -v '\[INFO\]'`; do jar tf $i | awk "{print \"$i\\t\" \$1}"; done | grep Matcher )
(网址:www.info.gov.hk)
它发现依赖性
JGlobus-Core-2.0.4
有自己的org.hamcrest.BaseMatcher
,org.hamcrest.CoreMatchers
,和org.hamcrest.Matcher
,如果您使用Eclipse,“开放式”工具(CTRL+SHIFT+T)可以帮助您找到有问题的包。只需搜索类名(例如,描述),同一个类在不同jar中多次出现就是危险信号。这个博客帮助我解决了同样的问题:
在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>
朱尼特
朱尼特
4.11
汉克雷斯特岩芯
org.hamcrest
对我有效的是重新排序依赖项。
而不是去mockito junit,
我必须把朱尼特,莫基托
Mockito 1.9.5使用的hamcrest 1.1不兼容,会导致问题。如果您使用的是Eclipse:
对我来说,在eclipse->project properties->Java构建路径中
将mockito-all-1.9.5.jar移到“订单和导出”列表的底部就成功了。在junit-4.11.jar和hamcrest-core-1.3.jar之上,我用下面的代码解决了我的
Gradle
项目中的jar地狱问题:
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'
对于使用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'
谢谢,我还不知道有这样的插件,但是你的回答激发了一个想法,将Maven的依赖插件、jar和grep结合起来。并在类路径中搜索BaseMatcher。周一的开始点不错。谢谢你的回答,我也遇到了同样的问题,你的解决方案对我来说也很有效。排除对我没有帮助,但lib的帮助顺序是:hamcrest dependency(library)应该在Junit和/或Mockito dependenciest之前。这似乎可以帮助注意!您不能使用mockito all,因为它里面有hamcrest(1.1),而不是作为依赖项。相反,使用mockito core和hamcrest core排除。您不需要从JUnit4.11中排除hamcrest,因为它依赖于hamcrest 1.3,只依赖于早期的junit版本。Mockito确实是一个令人不快的包。运行“mvn dependency:tree-Dscope=test”以将所有继承的依赖项根清除到mockito all会有所帮助-如果您正在处理一个大型项目,一些导入的模块可能会在您不知情的情况下使用它。仅供参考-您需要在Bash的末尾添加一个右括号command@HairOfTheDog增加了右括号。
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'
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'