【问题标题】:Eclipse: The type org.hamcrest.core.CombinableMatcher$CombinableBothMatcher cannot be resolved. It is indirectly referenced from required .class filesEclipse:无法解析类型 org.hamcrest.core.CombinableMatcher$CombinableBothMatcher。它是从所需的 .class 文件中间接引用的
【发布时间】:2015-10-22 15:44:49
【问题描述】:

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

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

以下是我的依赖项:

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

更新 1:

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

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

更新 2:

根据 Tunaki 的建议,更好的解决方案是从 mockito 库中排除传递依赖 hamcrest-core。所以最终的依赖应该如下所示:

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

【问题讨论】:

    标签: eclipse maven unit-testing mockito hamcrest


    【解决方案1】:

    你的 POM 中存在依赖冲突:

    • mockito-core 1.10.8 依赖于 hamcrest-core 1.1。
    • hamcrest-library 1.3 取决于 hamcrest-core 1.3。

    Maven 通过选择版本 1.1 (it is declared first and they have equal path) 解决了冲突。

    您收到此错误是因为 hamcrest-library 1.3 引用了 CombinableMatcherthat did not exist in version 1.1does exist in version 1.3

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

    【讨论】:

    • 感谢您的回答。我不确定它是否有效。但是当我用 hamcrest-all 替换 hamcrest-library 时,问题就消失了。谢谢。
    • @ManojShrestha 你实际上并没有消除问题,你只是隐藏了它。 hamcrest-all 是一个封装了所有 Hamcrest 依赖项(hamcrest-core 和 hamcrest-library)的 uber-jar 依赖项。由于对 hamcrest-core 没有实际依赖,Maven 无法检测到冲突,但它仍然存在。问题可能会再次出现,但现在在运行时。
    • 它没有抛出任何运行时错误。但我觉得你说的对我来说更有意义。我尝试从 mockito-core 中排除 hamcrest-core,它工作得很好。非常感谢您为我做研究。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2016-11-14
    • 2017-10-05
    • 2013-05-14
    • 2017-02-26
    • 2016-10-31
    • 2016-02-20
    相关资源
    最近更新 更多