Eclipse:无法解析org.hamcrest.core.CombinableMatcher $ CombinableBothMatcher类型.它是从所需的.class文件间接引用的

Man*_*tha 4 eclipse unit-testing hamcrest mockito maven

我正在Spring MVC使用TestNG和进行单元测试控制器Mockito.我Hamcrest在Maven依赖项中包含了库,如下所示.我在这里错过了什么?当我使用以下两种方法时出现错误:

org.hamcrest.Matchers.hasSize;
org.hamcrest.Matchers.is;
Run Code Online (Sandbox Code Playgroud)

以下是我的依赖项:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.8</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

更新1:

通过更改hamcrest-library为已解决问题hamcrest-all.

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

更新2:

根据Tunaki的建议,更好的解决方案是hamcrest-coremockito库中排除传递依赖.所以最终的依赖关系应如下所示:

<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>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Tun*_*aki 5

您的POM中存在依赖冲突:

  • mockito-core1.10.8取决于hamcrest-core1.1.
  • hamcrest-library1.3取决于hamcrest-core1.3.

Maven通过选择版本1.1来解决冲突(它首先被声明并且它们具有相同的路径).

您收到此错误,因为hamcrest-library1.3的引用的CombinableMatcher未在1.1版本中存在,但是在1.3版本中确实存在.

如果您真的依赖于Hamcrest 1.3特定功能,则需要排除hamcrest-core传递依赖性mockito-core(并希望Hamcrest 1.3向后兼容1.1).否则,只需删除hamcrest-library,然后您将依赖于Hamcrest 1.1.