Search code examples
extractassertj

AssertJ + Verify items in subset of list from the extracted list


So, have an use case where in I extract title from the object, as list say 10 items, now need to verify the title is present in anyof the first 5 items. Using below method to extract the items.But not sure how to reduce the list and verify it.

softAssertions.assertThat(resultArrayList)
                      .extracting("title")
                      .as("Title match")
                      .isEqualTo(placeToSearch);

Solution

  • There is no direct way to do this with AssertJ, I think the easiest solution would be to take the 5 first elements and simply use contains as in:

    softAssertions.assertThat(resultArrayList.subList(0, 5))
                      .extracting("title")
                      .as("Title match")
                      .contains(expectedTitle);
    

    Note that using isEqualTo does not work in your example unless it corresponds to the exact list of the expected titles.