Noe*_*Yap 222 java junit intellij-idea hamcrest junit4
我正在使用JUnit-dep 4.10和Hamcrest 1.3.RC2.
我创建了一个自定义匹配器,如下所示:
public static class MyMatcher extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
/* implementation */
}
@Override
public void describeTo(Description description) {
/* implementation */
}
@Override
protected void describeMismatchSafely(String item, Description mismatchDescription) {
/* implementation */
}
}
Run Code Online (Sandbox Code Playgroud)
使用Ant从命令行运行时,它完全正常.但是当从IntelliJ运行时,它失败了:
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.netflix.build.MyTest.testmyStuff(MyTest.java:40)
Run Code Online (Sandbox Code Playgroud)
我的猜测是它使用了错误的hamcrest.MatcherAssert.我如何找到它使用的hamcrest.MatcherAssert(即哪个jar文件用于hamcrest.MatcherAssert)?AFAICT,我班级路径中唯一的火腿罐是1.3.RC2.
IntelliJ IDEA使用它自己的JUnit或Hamcrest副本吗?
如何输出IntelliJ正在使用的运行时CLASSPATH?
Gar*_*all 265
确保导入顺序上的hamcrest jar比JUnit jar高.
JUnit带有自己的org.hamcrest.Matcher类,可能正在使用它.
你也可以下载并使用junit-dep-4.10.jar而不是没有hamcrest类的JUnit.
mockito也有hamcrest类,所以你可能需要移动\重新排序它
Tom*_*son 164
当您在类路径上具有mockito-all时,也会出现此问题,该路径已被弃用.
如果可能的话,只包括mockito-core.
混合junit,mockito和hamcrest的Maven配置:
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
Noe*_*Yap 58
问题是使用了错误的hamcrest.Matcher,而不是hamcrest.MatcherAssert类.这是从我的依赖项之一指定的junit-4.8依赖项中提取的.
要在测试时查看从哪个源包含哪些依赖项(和版本),请运行:
mvn dependency:tree -Dscope=test
Run Code Online (Sandbox Code Playgroud)
Ulf*_*ack 26
以下应该是今天最正确的.注意,junit 4.11依赖于hamcrest-core,因此您根本不需要指定mockito-all,因为它包含(不依赖于)hamcrest 1.1
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
Rau*_*aul 13
经过一番努力,这对我有用
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我知道这是一个旧线程,但为我解决问题的是将以下内容添加到我的 build.gradle 文件中。如上所述,存在与mockito-all
可能有用的帖子:
testCompile ('junit:junit:4.12') {
exclude group: 'org.hamcrest'
}
testCompile ('org.mockito:mockito-core:1.10.19') {
exclude group: 'org.hamcrest'
}
testCompile 'org.hamcrest:hamcrest-core:1.3'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99880 次 |
| 最近记录: |