unit-testing - AllOf Hamcrest 与 Maven 匹配器

标签 unit-testing maven hamcrest

正在运行

assertThat(collection, allOf(hasItems(i1, i2, i3), hasSize(3)));

从 Eclipse(运行方式 -> Junit)一切正常,但是当我执行 Maven 测试(mvn clean test)时,它在 test-compile 阶段失败,并显示以下解释

[ERROR] The method allOf(Matcher<? super T>, Matcher<? super T>) in the type AllOf<T> is not applicable for the arguments (Matcher<Iterable<Song>>, Matcher<Collection<? extends Object>>)

依赖项是

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit-dep</artifactId>
  <version>4.10</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <artifactId>hamcrest-core</artifactId>
      <groupId>org.hamcrest</groupId>
    </exclusion>
  </exclusions>
</dependency>

我做错了什么?

谢谢

斯特凡诺

最佳答案

您必须为方法 hasItems 指定类型参数和hasSize 。编译器的自动类型推断在您的情况下不起作用。要指定类型参数,您不得使用静态导入方法,而应使用其声明类来限定它们 - Matchers.<Song>hasItems(i1,i2,i3)例如。

关于unit-testing - AllOf Hamcrest 与 Maven 匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892635/

相关文章:

javascript - Jasmine 无法实例化 Angular 模块?

unit-testing - vstest.executionengine.exe 在 TFS 上崩溃,但在我的本地 VS2012 上一切正常

maven - 将 Selen 测试与构建过程分开

Maven:子模块不能继承父模块的依赖

unit-testing - 仅使用 golang 中的标准库断言特定错误

Swift:为 ViewController 中的私有(private)函数创建单元测试

java - 为什么在 junit5 测试中 Autowiring 的 Controller 总是为空?

java - 断言集合不包含项目

java - 如何断言字符串不为空

java - Hamcrest assertThat 在移动项目时不起作用