Export to GitHub

hamcrest - issue #192

IsMapWithSize


Posted on Apr 1, 2013 by Quick Wombat

java.util.Map is not a subtype of java.util.Collection

Please provide a way to match on its size.

Comment #1

Posted on Apr 1, 2013 by Quick Wombat

Comment deleted

Comment #2

Posted on Apr 1, 2013 by Quick Wombat

You have my permission to incorporate the source into your code base without mentioning my name if necessary.

Attachments

Comment #3

Posted on Apr 6, 2013 by Massive Hippo

(No comment was entered for this change.)

Comment #4

Posted on Apr 6, 2013 by Massive Hippo

You can achieve this using the existing matchers by using the map's entry set.

    final Map<String, String> map = new HashMap<String, String>();

    MatcherAssert.assertThat(map.entrySet(), Matchers.hasSize(3));

Comment #5

Posted on Apr 6, 2013 by Massive Hippo

Oh, and such a Matcher has already been written for inclusion in the next release of Hamcrest -- see https://github.com/hamcrest/JavaHamcrest/blob/master/hamcrest-library/src/main/java/org/hamcrest/collection/IsMapWithSize.java

When this is released, you'll be able to do:

    MatcherAssert.assertThat(map, Matchers.aMapWithSize(3));

Status: Fixed

Labels:
Java