Search code examples
javajunithamcrest

Any Hamcrest matcher to check if a collection is composed exactly of x,y elements, unregardless of order and cardinality


I am using hamcrest Matchers and I don't find contains, containsInAnyOrder, hasItems and containsInRelativeOrder solve that need.

I need a matcher like:

  • Collection B is composed of exactly the same elements as Collection A in any order and cardinality

Its some kind of hasExactlyItems() or containsInAnyOrderAndCardinality()


Solution

  • I would recommend to use AssertJ for this purpose. It has lot of fluent assertions for the collections like:

    • containsExactlyInAnyOrderElementsOf
    • containsExactlyElementsOf
    • containsExactlyInAnyOrder etc.,