正在运行
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/