Eclipse: The type org.hamcrest.core.CombinableMatcher$CombinableBothMatcher cannot be resolved. It is indirectly referenced from required .class files

微笑、不失礼 提交于 2019-12-10 15:38:43

问题


I was working on a unit testing for a Spring MVC controller using TestNG and Mockito. I've included Hamcrest library in the Maven dependencies as shown below. What am I missing here? The error shows up when I use the following two methods:

org.hamcrest.Matchers.hasSize;
org.hamcrest.Matchers.is;

The following are my dependencies:

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

UPDATE 1:

The problem has been resolved by changing hamcrest-library to hamcrest-all.

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

UPDATE 2:

As per suggested by Tunaki, the better solution would be to exclude the transitive dependency hamcrest-core from mockito library. So the final dependencies should look something like as follows:

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

回答1:


There is a dependency conflict in your POM:

  • mockito-core 1.10.8 depends on hamcrest-core 1.1.
  • hamcrest-library 1.3 depends on hamcrest-core 1.3.

Maven resolves the conflict by selecting version 1.1 (it is declared first and they have equal path).

You are getting this error because hamcrest-library 1.3 references the CombinableMatcher class that did not exist in version 1.1 but does exist in version 1.3.

If you really depend on Hamcrest 1.3 specific features, you need to exclude hamcrest-core transitive dependency from mockito-core (and hope Hamcrest 1.3 is backwards compatible with 1.1). Otherwise, just remove hamcrest-library and you will then depend on Hamcrest 1.1.



来源:https://stackoverflow.com/questions/33285143/eclipse-the-type-org-hamcrest-core-combinablematchercombinablebothmatcher-cann

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!