有时就足够了。但有时你不知道测试中的代码返回的类型,所以你不知道是否会正确地将代码返回的未知类型的映射与你构造的映射进行比较。或者您不想将代码与此类测试绑定。Map.equals()
Map
.equals()
此外,单独构建地图以将结果与结果进行比较恕我直言,这并不是很优雅:
Map<MyKey, MyValue> actual = methodUnderTest();
Map<MyKey, MyValue> expected = new HashMap<MyKey, MyValue>();
expected.put(new MyKey(1), new MyValue(10));
expected.put(new MyKey(2), new MyValue(20));
expected.put(new MyKey(3), new MyValue(30));
assertThat(actual, equalTo(expected));
我更喜欢使用马切尔斯:
import static org.hamcrest.Matchers.hasEntry;
Map<MyKey, MyValue> actual = methodUnderTest();
assertThat(actual, allOf(
hasSize(3), // make sure there are no extra key/value pairs in map
hasEntry(new MyKey(1), new MyValue(10)),
hasEntry(new MyKey(2), new MyValue(20)),
hasEntry(new MyKey(3), new MyValue(30))
));
我必须定义自己:hasSize()
public static <K, V> Matcher<Map<K, V>> hasSize(final int size) {
return new TypeSafeMatcher<Map<K, V>>() {
@Override
public boolean matchesSafely(Map<K, V> kvMap) {
return kvMap.size() == size;
}
@Override
public void describeTo(Description description) {
description.appendText(" has ").appendValue(size).appendText(" key/value pairs");
}
};
}
还有另一种变体,它将匹配器作为参数,而不是键和值的精确值。如果您需要对每个键和值进行相等性测试以外的其他操作,这可能很有用。hasEntry()