Multiple correct results with Hamcrest (is there an or-matcher?) Multiple correct results with Hamcrest (is there an or-matcher?) java java

Multiple correct results with Hamcrest (is there an or-matcher?)


assertThat(result, anyOf(equalTo(1), equalTo(2), equalTo(3)))

From Hamcrest tutorial:

anyOf - matches if any matchers match, short circuits (like Java ||)

See also Javadoc.

Moreover, you could write your own Matcher, what is quite easy to do.


marcos is right, but you have a couple other options as well. First of all, there is an either/or:

assertThat(result, either(is(1)).or(is(2)));

but if you have more than two items it would probably get unwieldy. Plus, the typechecker gets weird on stuff like that sometimes. For your case, you could do:

assertThat(result, isOneOf(1, 2, 3))

or if you already have your options in an array/Collection:

assertThat(result, isIn(theCollection))

See also Javadoc.