How to check whether a string is one of the given values using assertj?

How to check in a simpler way whether a string matches one of the given options?

List<String> OPTIONS = Arrays.asList("alpha", "beta", "gamma");
String text = "beta";
assertThat(OPTIONS.stream().anyMatch(o -> o.equals(text))).isTrue();

Upvotes: 0

Views: 1444

Answers (1)

There is a built-in solution in AssertJ: org.assertj.core.api.AbstractAssert::isIn

@Test
public void testOneOf() {
  List<String> OPTIONS = Arrays.asList("alpha", "beta", "gamma");

  String text = "beta";

  Assertions.assertThat(text)
    .as("Should contain one value from OPTIONS")
    .isIn(OPTIONS);
}

Upvotes: 2

Related Questions