AllOf Hamcrest匹配Maven

时间:2012-12-15 13:30:30

标签: 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>

我做错了什么?

由于

斯特凡诺

1 个答案:

答案 0 :(得分:2)

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