【发布时间】:2016-10-21 12:40:51
【问题描述】:
我正在使用 Hamcrest 1.3 并尝试以更紧凑的方式实现以下目标。
考虑以下测试用例:
@Test
public void testCase() throws Exception {
Collection<String> strings = Arrays.asList(
"string one",
"string two",
"string three"
);
// variant 1:
assertThat(strings, hasSize(greaterThan(2)));
assertThat(strings, hasItem(is("string two")));
// variant 2:
assertThat(strings, allOf(
hasSize(greaterThan(2)),
hasItem(is("string two"))
));
}
这里的目标是检查集合的大小和要包含的一些特定项目。
在第一个变化是可能的并且被接受的情况下,它并不总是那么容易做到,因为也许集合本身就是一些其他操作的结果,因此使用@987654323对其进行所有操作更有意义@ 手术。这是在上面的第二个变体中完成的。
但是包含第二个变体的代码会导致以下编译时错误:
error: no suitable method found for allOf(Matcher<Collection<? extends Object>>,Matcher<Iterable<? extends String>>)
实际上是否有任何特定的方法可以使用单次操作(如 allOf)在 Hamcrest 中测试集合的大小和项目?
【问题讨论】:
-
@rvazquezglez 相关,因为它们都与 hamcrest 和收藏有关;)否则请重新阅读标题!