Map<Integer, Map<String, String>> mapMap = new HashMap<Integer,Map<String, String>>();
目前这样断言
assertThat(mapMap.size(), is(equalTo(1)));
Or
assertThat(mapMap.values(), hasSize(1));
有没有其他方法,例如与列表一起使用的方法。
assertThat(someListReferenceVariable, hasSize(1));
最佳答案
好消息
在当前 master branch of the JavaHamcrest project 中有一个匹配器可以完全满足您的需求。 . 你可以这样调用它:
assertThat(mapMap, aMapWithSize(1));
还有坏消息
很遗憾,这个匹配器不在最新版本的 Hamcrest (1.3) 中。
[更新] 最后是好消息
aforementioned matcher is included在新发布的 2.1 版本中。
关于java - 如何使用 Hamcrest 验证 map 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36030650/