我使用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)?AFAICT,我的类路径中唯一的hamcrest jars是1.3.RC2。

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

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


确保hamcrest罐在进口顺序上高于JUnit罐。

JUnit自带自己的org.hamcrest.Matcher类,这个类可能正在被使用。

您也可以下载并使用JUnit -dep-4.10.jar,它是没有hamcrest类的JUnit。

Mockito也有hamcrest类,所以你可能需要移动\重新排序


问题是戴错了膝盖骨。Matcher,不是hamcrest。MatcherAssert,类正在被使用。它是从junit-4.8依赖项中拉进来的,我的一个依赖项正在指定。

要在测试时查看来自哪个源的依赖项(和版本),请运行:

mvn dependency:tree -Dscope=test

当类路径上有mockito-all时,这个问题也会出现,这已经被弃用了。

如果可能的话,只包含模拟核。

Maven配置混合junit, mockito和hamcrest:

<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>

在经历了一些挣扎后,这对我来说很有效

<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>

以下应该是今天最正确的。注意,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>

Try

expect(new StringContains(message))

而不是

expectMessage(消息)

您可以编写自定义ExpectedException或实用程序方法来封装代码。


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

下面是build.gradle的代码:

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

如果你正在运行IntelliJ,你可能需要运行gradle cleanIdea idea clean build来再次检测依赖项。


我知道这不是最好的答案,但是如果您不能让类路径工作,这是一个B计划解决方案。

在我的测试类路径中,我为descripbemismatch方法添加了一个默认实现的接口。

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_();
}

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

尽管这是一个非常古老的问题 也许前面提到的许多想法解决了许多问题, 我仍然想与解决我的问题的社区分享解决方案。

我发现问题出在一个叫hasItem的函数上 我用它来检查json数组是否包含特定的项。 在我的例子中,我检查了类型为Long的值。

这就产生了问题。

不知何故,matcher在Long类型的值上有问题。 (我不太使用JUnit或Rest-Assured,所以idk。为什么, 但我猜返回的JSON-data只包含整数。)

为了解决这个问题,我做了如下的事情。 而不是使用:

long ID = ...;

...
.then().assertThat()
  .body("myArray", hasItem(ID));

你只需要转换为Integer。 工作代码是这样的:

long ID = ...;

...
.then().assertThat()
  .body("myArray", hasItem((int) ID));

这可能不是最好的解决方案, 但我只是想提一下,也可以因为错误/未知的数据类型而抛出异常。


我有一个gradle项目,当我建立。Gradle依赖项部分如下所示:

dependencies {
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'

    testImplementation group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
    testImplementation 'junit:junit:4.12'
//    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.23.4'

    compileOnly 'org.projectlombok:lombok:1.18.4'
    apt 'org.projectlombok:lombok:1.18.4'
}

它导致了这个例外:

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)

为了解决这个问题,我将“mockito-all”替换为“mockito-core”。

dependencies {
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'

//    testImplementation group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
    testImplementation 'junit:junit:4.12'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.23.4'

    compileOnly 'org.projectlombok:lombok:1.18.4'
    apt 'org.projectlombok:lombok:1.18.4'
}

mockito-all和mockito-core之间的解释可以在这里找到: https://solidsoft.wordpress.com/2012/09/11/beyond-the-mockito-refcard-part-3-mockito-core-vs-mockito-all-in-mavengradle-based-projects/

mockito-all.jar besides Mockito itself contains also (as of 1.9.5) two dependencies: Hamcrest and Objenesis (let’s omit repackaged ASM and CGLIB for a moment). The reason was to have everything what is needed inside an one JAR to just put it on a classpath. It can look strange, but please remember than Mockito development started in times when pure Ant (without dependency management) was the most popular build system for Java projects and the all external JARs required by a project (i.e. our project’s dependencies and their dependencies) had to be downloaded manually and specified in a build script. On the other hand mockito-core.jar is just Mockito classes (also with repackaged ASM and CGLIB). When using it with Maven or Gradle required dependencies (Hamcrest and Objenesis) are managed by those tools (downloaded automatically and put on a test classpath). It allows to override used versions (for example if our projects uses never, but backward compatible version), but what is more important those dependencies are not hidden inside mockito-all.jar what allows to detected possible version incompatibility with dependency analyze tools. This is much better solution when dependency managed tool is used in a project.


就我而言,我不得不从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>

这对我很管用。没有必要排除任何东西。我只是用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'

截至2020年7月,pom.xml中的以下依赖项对我有效:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.1</version>
</dependency>

在这个4.13 junit库和hamcrest中,它使用了hamcrest。在断言并抛出异常时使用MatcherAssert 在这里输入图像描述


对于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>