assert collection does not contain item

JavaHamcrest

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Negate the hasItem assertion

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

Solution 2 - Java

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();

Solution 3 - Java

or you can do;

.extract().jsonPath().getObject("data", pojo.class);  
(above is response)

assertThat(response, not(hasItem(bodyYouArePosting)));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionharschwareView Question on Stackoverflow
Solution 1 - Javadee-seeView Answer on Stackoverflow
Solution 2 - JavaSashkoView Answer on Stackoverflow
Solution 3 - JavaMustafa OkurView Answer on Stackoverflow