JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

contains matcher should also support empty set evaluation or alternatively we should have one that can

Open mat013 opened this issue 8 months ago • 0 comments

i have test case where the outcome returns empty set however I cannot use contains matcher as it throws following exception

Caused by: java.lang.IllegalArgumentException: Should specify at least one expected element at org.hamcrest.collection.IsIterableContainingInOrder$MatchSeries.(IsIterableContainingInOrder.java:46)

The issue is not when you have a single test where one can test for isEmpty which i think would be more transparent

however if you want to run datadriven test where one of the test cases is that the outcome is an empty set, then it would be simplier to reuse the same test for that testcase rather than needing to execute those in a separate test

Alternatively to this one should introduce a new matcher which could (containsIncludingEmpty?) accept an empty set

The only workaround I could use if I did not have to implement is containsInAnyOrder but that does not take order into consideration And example in spock

void 'unsupported'() {
  when:
     true
   then:
       that(given, contains(given.toArray()))
   where:
       given   | _
       ['abc'] | _
       []      | _

}

mat013 avatar Jun 13 '25 07:06 mat013