How to assert an actual value against 2 or more expected values?
I am using the following, I hope this would help:
String expectedTitles[] = {"Expected1","Expected2"};
List<String> expectedTitlesList = Arrays.asList(expectedTitles);
assertTrue(expectedTitlesList.contains((actualTitle)));
Using the Hamcrest CoreMatcher
(included in JUnit 4.4 and later) and assertThat()
:
assertThat(myString, anyOf(is("value1"), is("value2")));
I would use AssertJ for this:
import static org.assertj.core.api.Assertions.*;
assertThat("hello").isIn("hello", "world");
It's more concise and it will give you a descriptive message when the assertion fails.