Java 如何使用Hamcrest验证地图大小

Java 如何使用Hamcrest验证地图大小,java,junit,hamcrest,Java,Junit,Hamcrest,还有其他类似于列表的方法吗 资产(someListReferenceVariable,hasSize(1)) 好消息 有一个匹配器,它在当前应用程序中完全满足您的需要。 你可以这样称呼它: assertThat(mapMap.size(), is(equalTo(1))); Or assertThat(mapMap.values(), hasSize(1)); 还有坏消息 不幸的是,Hamcrest(1.3)的最新版本中没有此匹配器 [Update]还有最后一个好消息 最新发布的2.1版中没有

还有其他类似于列表的方法吗

资产(someListReferenceVariable,hasSize(1))


好消息

有一个匹配器,它在当前应用程序中完全满足您的需要。 你可以这样称呼它:

assertThat(mapMap.size(), is(equalTo(1)));
Or
assertThat(mapMap.values(), hasSize(1));
还有坏消息

不幸的是,Hamcrest(1.3)的最新版本中没有此匹配器

[Update]还有最后一个好消息


最新发布的2.1版中没有。Hamcrest 1.3中没有,但您可以非常轻松地创建自己的:

assertThat(mapMap, aMapWithSize(1));
* *@param-sizeMatcher *检查的{@link java.util.Map}大小的匹配器 */ @工厂
publicstaticmatcher您可以使用not和any进行检查


实际上,与其说是莫基托问题,不如说是汉克雷斯特问题。因此,您可以查看Hamcrest文档。如果您想要的匹配器不存在(我相信它不存在),您可以随时编写自己的。使用entryset()方法,然后您可以使用大小进行验证。它包括一个空映射的匹配器
anEmptyMap()
,感谢您的链接。这帮助我用Rest-Assured验证映射是否为空
assertThat(mapMap, aMapWithSize(1));
public class IsMapWithSize<K, V> extends FeatureMatcher<Map<? extends K, ? extends V>, Integer> {
    public IsMapWithSize(Matcher<? super Integer> sizeMatcher) {
        super(sizeMatcher, "a map with size", "map size");
    }

    @Override
    protected Integer featureValueOf(Map<? extends K, ? extends V> actual) {
        return actual.size();
    }

    /**
     * Creates a matcher for {@link java.util.Map}s that matches when the
     * <code>size()</code> method returns a value that satisfies the specified
     * matcher.
     * <p/>
     * For example:
     * 
     * <pre>
     * Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
     * map.put(&quot;key&quot;, 1);
     * assertThat(map, isMapWithSize(equalTo(1)));
     * </pre>
     * 
     * @param sizeMatcher
     *            a matcher for the size of an examined {@link java.util.Map}
     */
    @Factory
    public static <K, V> Matcher<Map<? extends K, ? extends V>> isMapWithSize(Matcher<? super Integer> sizeMatcher) {
        return new IsMapWithSize<K, V>(sizeMatcher);
    }

    /**
     * Creates a matcher for {@link java.util.Map}s that matches when the
     * <code>size()</code> method returns a value equal to the specified
     * <code>size</code>.
     * <p/>
     * For example:
     * 
     * <pre>
     * Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
     * map.put(&quot;key&quot;, 1);
     * assertThat(map, isMapWithSize(1));
     * </pre>
     * 
     * @param size
     *            the expected size of an examined {@link java.util.Map}
     */
    @Factory
    public static <K, V> Matcher<Map<? extends K, ? extends V>> isMapWithSize(int size) {
        Matcher<? super Integer> matcher = equalTo(size);
        return IsMapWithSize.<K, V> isMapWithSize(matcher);
    }

}
    Map<String, Integer> map = new HashMap<>();
    map.put("key", 1);
    assertThat(map, isMapWithSize(1));
    assertThat(map, isMapWithSize(equalTo(1)));
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.not;

not(hasEntry(any(Object.class), any(Object.class)))