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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36030650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:53:45  来源:igfitidea点击:

How to verify Map size using Hamcrest

javajunithamcrest

提问by Ramesh

Map<Integer, Map<String, String>> mapMap = new HashMap<Integer,Map<String, String>>();

Currently asserting like this

目前这样断言

assertThat(mapMap.size(), is(equalTo(1)));
Or
assertThat(mapMap.values(), hasSize(1));

Are there any other methods like one used with Lists.

是否有任何其他方法,例如与 Lists 一起使用的方法。

assertThat(someListReferenceVariable, hasSize(1));

assertThat(someListReferenceVariable, hasSize(1));

回答by eee

The good news

好消息

There is a matcher that does exactly what you want in the current master branch of the JavaHamcrest project. You can call it like so:

有一个匹配器可以在 JavaHamcrest 项目的当前主分支中执行您想要的操作。你可以这样称呼它:

assertThat(mapMap, aMapWithSize(1));

And the bad news

还有坏消息

Unfortunately this matcher is not in the latest release of Hamcrest (1.3).

不幸的是,这个匹配器不在最新版本的 Hamcrest (1.3) 中。

[Update] And finally the very good news

[更新] 最后一个好消息

The aforementioned matcher is includedin the newly released version 2.1.

上述匹配包括在新发布的2.1版本。

回答by Ruben

There is none in Hamcrest 1.3, but you can very easily create your own:

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

}

Testing:

测试:

    Map<String, Integer> map = new HashMap<>();
    map.put("key", 1);
    assertThat(map, isMapWithSize(1));
    assertThat(map, isMapWithSize(equalTo(1)));

回答by Declow0

You can check this using not and any

您可以使用 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)))