你会如何简洁地断言Collection元素的相等性,特别是Set在JUnit 4中?
为什么这不编译,哦,该怎么办?
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItems;
ArrayList<Integer> actual = new ArrayList<Integer>();
ArrayList<Integer> expected = new ArrayList<Integer>();
actual.add(1);
expected.add(2);
assertThat(actual, hasItems(expected));
Run Code Online (Sandbox Code Playgroud)
从评论中复制的错误:
cannot find symbol method assertThat(java.util.ArrayList<java.lang.Integer>, org.hamcreset.Matcher<java.lang.Iterable<java.util.ArrayList<java.lang.Integer>>>)
Run Code Online (Sandbox Code Playgroud) Hamcrest提供了许多用于断言集合内容的匹配器.所有这些案件都通过:
Collection<String> c = ImmutableList.of("one", "two", "three");
assertThat(c, hasItems("one", "two", "three");
assertThat(c, contains("one", "two", "three");
assertThat(c, containsInAnyOrder("one", "two", "three");
Run Code Online (Sandbox Code Playgroud)
如何hasItems,contains以及containsInAnyOrder有什么不同?