使用Hamcrest的地图平等 [英] Map equality using Hamcrest
问题描述
我想使用hamcrest断言两个地图是相等的,即它们具有相同的键集合指向相同的值。
I'd like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values.
我当前最好的guess is:
My current best guess is:
assertThat( affA.entrySet(), hasItems( affB.entrySet() );
其中:
assertThat(T,Matcher)类型Assert不适用于参数(Set>,Matcher >>>)
The method assertThat(T, Matcher) in the type Assert is not applicable for the arguments (Set>, Matcher>>>)
ve还可以看到containsAll的变体,以及其他一些由hamcrest包提供的方法。任何人都可以指向正确的方向,或者我必须写一个自定义的匹配器吗?
I've also looked into variations of containsAll, and some others provided by the hamcrest packages. Can anyone point me in the right direction? Or do I have to write a custom matcher?
推荐答案
我想出的最短的方法是两个语句:
The shortest way I've come up with is two statements:
assertThat( affA.entrySet(), everyItem(isIn(affB.entrySet())));
assertThat( affB.entrySet(), everyItem(isIn(affA.entrySet())));
但你也可以这样做:
assertThat(affA.entrySet(), equalTo(affB.entrySet()));
取决于地图的实现。
这篇关于使用Hamcrest的地图平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!