【问题标题】:How to verify Map size using Hamcrest如何使用 Hamcrest 验证地图大小
【发布时间】:2023-03-28 00:52:02
【问题描述】:
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));

【问题讨论】:

  • 实际上更多的是 Hamcrest 问题而不是 Mockito 问题。所以你可以查看 Hamcrest 文档。如果您想要的匹配器不存在(我相信它不存在),您可以随时编写自己的匹配器。
  • 使用entryset()方法,然后就可以用size来验证了。

标签: java junit hamcrest


【解决方案1】:

好消息

在当前master branch of the JavaHamcrest project 中有一个匹配器可以完全满足您的需求。 你可以这样称呼它:

assertThat(mapMap, aMapWithSize(1));

还有坏消息

很遗憾,这个匹配器不在最新版本的 Hamcrest (1.3) 中。

[更新] 最后是好消息

新发布的2.1版本中的aforementioned matcher is included

【讨论】:

  • 它包括一个空地图匹配器! anEmptyMap(),感谢您的链接。
【解决方案2】:

Hamcrest 1.3 中没有,但您可以非常轻松地创建自己的:

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)));

【讨论】:

    【解决方案3】:

    您可以使用 not 和 any 进行检查

    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)))
    

    【讨论】:

    • 这帮助我验证了地图是否为空,并提供了 Rest Assured
    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多