harschware
harschware

Reputation: 13404

assert collection does not contain item

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

Upvotes: 88

Views: 62047

Answers (3)

Mustafa Okur
Mustafa Okur

Reputation: 1

or you can do;

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

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

Upvotes: -3

Sashko
Sashko

Reputation: 191

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

Upvotes: 7

dee-see
dee-see

Reputation: 24078

Negate the hasItem assertion

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

Upvotes: 158

Related Questions