在IntelliJ 10.5中运行测试时出现“NoSuchMethodError: org.hamcrest.Matcher.describeMismatch”的错误。

245

我正在使用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 */
    }
}

在使用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)

我猜测它正在使用错误的 hamcrest.MatcherAssert。 我如何找出它正在使用哪个 hamcrest.MatcherAssert(即哪个 jar 文件用于 hamcrest.MatcherAssert)? 据我所知,我的类路径中唯一的 hamcrest jars 是 1.3.RC2。

IntelliJ IDEA 是否在使用自己的 JUnit 或 Hamcrest 副本?

如何输出 IntelliJ 正在使用的运行时 CLASSPATH?

15个回答

1
在我的情况下,我必须从junit-vintage中排除一个较旧的hamcrest:
<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest</artifactId>
  <version>2.1</version>
  <scope>test</scope>
</dependency>

1
这对我很有帮助。不需要排除任何内容。我只是使用了mockito-core而不是mockito-all
testCompile 'junit:junit:4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.0.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '2.1'

0

对我有效的方法是从junit测试编译中排除hamcrest组。

这是我的build.gradle代码:

testCompile ('junit:junit:4.11') {
    exclude group: 'org.hamcrest'
}

如果您正在运行 IntelliJ,可能需要运行 gradle cleanIdea idea clean build 以重新检测依赖项。

0

我知道这不是最好的答案,但如果你无法让classpath正常工作,这是一个备选方案。

在我的测试classpath中,我添加了以下接口,并提供了一个默认实现来描述describeMismatch方法。

package org.hamcrest;

/**
 * PATCH because there's something wrong with the classpath. Hamcrest should be higher than Mockito so that the BaseMatcher
 * implements the describeMismatch method, but it doesn't work for me. 
 */
public interface Matcher<T> extends SelfDescribing {

    boolean matches(Object item);

    default void describeMismatch(Object item, Description mismatchDescription) {
        mismatchDescription.appendDescriptionOf(this).appendValue(item);
    }

    @Deprecated
    void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}

0

对于 jUnit 4.12,以下依赖组合解决了我的问题。

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接