Search code examples
javaunit-testingassertj

AssertJ - how to check that an element is in a collection?


Is there a way to check that an object is an element of some collection with AssertJ? Something like

assertThat(actualObject).isElementOf(collectionWithExpectedOptions);

I could do

assertThat(collectionWithExpectedOptions).contains(actualObject);

But I think there is a convention that the argument of assertThat() should be the actual rather than the expected thing.


Solution

  • try isIn:

    Iterable<Ring> elvesRings = list(vilya, nenya, narya);
    
    // assertion will pass:
    assertThat(nenya).isIn(elvesRings);