Although English is not my mother tongue, I still think that the following error message from Hamcrest is wrong.
I have the following validation method:
private void validateReturnedCode() {
List<HTTPStatusCodes> expectedStatusCodesList = requestData.getExpectedHttpCodes().getStatusCodes();
HTTPStatusCodes actualReturnedCode = fromCode(response.getStatusCode());
assertThat(expectedStatusCodesList, hasItem(actualReturnedCode));
}
Suppose expectedStatusCodesList is {REDIRECT_FOUND} and actualReturnedCode is OK.
So obviously there should be an exception here, but I cannot understand the logic of the error message:
java.lang.AssertionError:
Expected: a collection containing <OK>
but: mismatches were: [was <REDIRECT_FOUND>]
Wouldn't it be better if it was:
java.lang.AssertionError:
Expected: a status code one of <REDIRECT_FOUND>
but: mismatches were: [value was <OK>]
?
The expectation was to receive REDIRECT_FOUND but actually I received OK!
If I get it wrong, could you please explain?
The first argument for the assertThat()
method states, that it must be the "actual" value you get:
assertThat
public static <T> void assertThat(T actual, Matcher<? super T> matcher)
So, your actualReturnedCode
must be in the front. Use the isIn()
match to check if a value is inside a collection:
assertThat(actualReturnedCode, isIn(expectedStatusCodesList));