Search code examples
javajunitjunit4assert

JUnit Test With More than One Accepted Answer


Is it possible to have multiple accepted answers in a JUnit test? So for example:

   Main rock = new Main();
   Assert.assertEquals("y", boxes("23"));

So for this test I want to be able to accept the result string "y" or "n" as an acceptable answer, is that possible?


Solution

  • Using the Hamcrest CoreMatcher (included in JUnit 4.4 and later) and assertThat():

    Assert.assertThat(boxes("23"), anyOf(is("y"), is("n"));