【问题标题】:AllOf Hamcrest matcher with MavenAllOf Hamcrest 匹配器与 Maven
【发布时间】:2023-04-04 06:50:01
【问题描述】:

跑步

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

从 Eclipse (Run as -> 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>

我做错了什么?

谢谢

斯特凡诺

【问题讨论】:

    标签: unit-testing maven hamcrest


    【解决方案1】:

    您必须为方法hasItemshasSize 指定类型参数。编译器的自动类型推断不适用于您的情况。要指定类型参数,您不能使用静态导入方法,而是使用它们的声明类来限定它们 - 例如 Matchers.&lt;Song&gt;hasItems(i1,i2,i3)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多