在IntelliJ 10.5中运行测试时获取"NoSuchMethodError:org.hamcrest.Matcher.describeMismatch"

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类,所以你可能需要移动\重新排序它

  • 确保你不使用mockito-all,而是使用mockito-core而不使用hamcrest (22认同)
  • JUnit 4.11与Hamcrest 1.3兼容,JUnit 4.10与Hamcrest 1.1兼容http://search.maven.org/remotecontent?filepath=junit/junit-dep/4.10/junit-dep-4.10.pom (8认同)
  • 我删除了IntelliJ的junit.jar和junit-4.8.jar的副本,将junit-dep-4.10.jar安装到IntelliJ的lib /目录中,问题仍然存在. (2认同)
  • “进口较高”是什么意思? (2认同)

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)

  • 应该是mockito-core而不是mockito-all? (3认同)
  • 你可以只包括核心,如果你只需要它的速度,但上述应该适用于所有情况.依赖关系的顺序是重要的位mvn 3按优先级顺序从顶部开始. (3认同)
  • 你不应该包括mockito-all,因为它包括hamcrest 1.1,而不是包括mockito-core并从中排除hancrest(你无法做到) (3认同)
  • 新版本的mockito包括hamcrest也与powermock相同! (2认同)
  • “如果可能的话,只包括mockito-core。”。好的,那么为什么这个答案仍然使用mockito-all呢? (2认同)

Noe*_*Yap 58

问题是使用了错误的hamcrest.Matcher,而不是hamcrest.MatcherAssert类.这是从我的依赖项之一指定的junit-4.8依赖项中提取的.

要在测试时查看从哪个源包含哪些依赖项(和版本),请运行:

mvn dependency:tree -Dscope=test
Run Code Online (Sandbox Code Playgroud)

  • mockito-all还包括一些Hamcrest类.最好使用mockito-core并排除hamcrest依赖. (9认同)
  • 我遇到过同样的问题.我使用的是JUnit-dep和Hamcrest-core,但我之前在pom中列出了Powermock,导致JUnit被包含在JUnit-dep和Hamcrest之前. (5认同)
  • 只是偶然发现了同样的问题.解决方案是将junit版本升级到4.11,它与hamcrest 1.3兼容(即"包含来自"的类) (3认同)
  • 对我来说,当我将导入从“import static org.mockito.ArgumentMatchers.anyString;”更改为“import static org.mockito.Matchers.anyString;”时,它就起作用了。 (2认同)

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)

  • 请注意,JUnit 4.12现在依赖于hamcrest-core 1.3. (3认同)

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)


Kai*_*Kai 5

我知道这是一个旧线程,但为我解决问题的是将以下内容添加到我的 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)