How to verify Map size using Hamcrest
Asked Answered
S

3

35
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.

assertThat(someListReferenceVariable, hasSize(1));

Squirearchy answered 16/3, 2016 at 8:48 Comment(2)
Really more a Hamcrest question than a Mockito question. So you could check the Hamcrest documentation. If the matcher you want isn't there (and I believe it isn't), you could always write your own.Va
Use entryset() method and then you can verify with size.Brunhild
P
37

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:

assertThat(mapMap, aMapWithSize(1));

And the bad news

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

[Update] And finally the very good news

The aforementioned matcher is included in the newly released version 2.1.

Philter answered 17/3, 2016 at 20:32 Comment(1)
And it includes a matcher for empty maps! anEmptyMap(), thanks for the link.Dank
P
5

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

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)));
Piegari answered 18/3, 2016 at 14:37 Comment(0)
C
1

You can check this using not and 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)))
Cosmopolitan answered 26/11, 2019 at 13:39 Comment(1)
That helped me to verify that a map is empty with Rest AssuredGilbert

© 2022 - 2024 — McMap. All rights reserved.