assert collection does not contain item

harschware picture harschware · Feb 20, 2012 · Viewed 41.6k times · Source

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

Answer

dee-see picture dee-see · Feb 20, 2012

Negate the hasItem assertion

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