AssertJ + verify items returned in subList contains string as will be present in any of the list items

Viewed 257

I have requirement, say in my response I get the results in list as "brisbane, australia", "sydney, australia" and "melbourne, australia". Now I need to test that, the results contains "sydney" in any of the three results.

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

Kindly suggest.

1 Answers

This can be done with allSatisfy.

softAssertions.assertThat(resultArrayList.subList(0, 3))
          .extracting("title")
          .as("Title match")
          // assuming titles are String, use contains String assertion
          .allSatisfy(cities -> cities.contains("sydney"));
Related