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

编程入门 行业动态 更新时间:2024-10-24 17:32:33
本文介绍了Eclipse:无法解析org.hamcrest.core.CombinableMatcher $ CombinableBothMatcher类型.它是从所需的.class文件中间接引用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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;

以下是我的依赖项:

<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,此问题已解决.

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>

更新2:

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

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>

推荐答案

您的POM中存在依赖项冲突:

There is a dependency conflict in your POM:

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

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

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

您收到此错误,因为hamcrest-library 1.3引用了CombinableMatcher类在版本1.1中不存在,但版本1.3中确实存在.

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.

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

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.

更多推荐

Eclipse:无法解析org.hamcrest.core.CombinableMatcher $ CombinableBothMatcher类型.它是从所需的

本文发布于:2023-11-04 01:07:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是从   所需   类型   文件   hamcrest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!