Question

Using the hamcrest library for Java, what's a nicely readable way to do the opposite of:

assertThat(someCollection, hasItem(someItem))

I want to make sure someCollection does not contain item someItem

Was it helpful?

Solution

Negate the hasItem assertion

assertThat(someCollection, not(hasItem(someItem)))

OTHER TIPS

If you need to Assert an Array, the same logic use not(hasItemInArray())

final String[] availableIds = {"123", "321"};
final String userId = "333";

softAssert.assertThat("Id not found", availableIds, not(hasItemInArray(userId)));
softAssert.assertAll();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top