Issue
I'm testing a method to see if it returns the correct string. This string is made up of a lot of lines whose order might change, thus usually giving 2 possible combinations. That order is not important for my application.
However, because the order of the lines might change, writing just an Assert statement will not work, since sometimes it will pass the test, and sometimes it will fail the test.
So, is it possible to write a test that will assert an actual string value against 2 or more expected string values and see if it is equal to any of them?
Solution
Using the Hamcrest CoreMatcher
(included in JUnit 4.4 and later) and assertThat()
:
assertThat(myString, anyOf(is("value1"), is("value2")));
Answered By - Joachim Sauer