isah's solution is perfect for you if you want to confirm that the set contains exactly the two items you specify; Mockito compares using .equals
by default, and Set.equals
is defined as refer to equal elements in any order.
For a more-flexible "contains" test that matches your question title, that allows for set members beyond your expected values, you can also use the Hamcrest contains
matcher:
someLogic.action();
verify(specificService).handleArgs(argThat(contains("first", "second")));
At least, that's how it should look. Unfortunately, argThat
infers its return type from the Matcher, which infers its return type from the arguments, so Java assumes your first argument is not a Set<String>
but a Iterable<capture#1-of ? extends String>
. You'll need to cast explicitly and suppress warnings to get it to work:
// requires @SuppressWarnings("unchecked")
verify(specificService).handleArgs(
(Set<String>) argThat(contains("first", "second")));