Hamcrest:异常消息不明确?
Hamcrest: Unclear exception message?
虽然英语不是我的母语,但我仍然认为 Hamcrest 的以下错误信息是错误的。
我有以下验证方法:
private void validateReturnedCode() {
List<HTTPStatusCodes> expectedStatusCodesList = requestData.getExpectedHttpCodes().getStatusCodes();
HTTPStatusCodes actualReturnedCode = fromCode(response.getStatusCode());
assertThat(expectedStatusCodesList, hasItem(actualReturnedCode));
}
假设 expectedStatusCodesList 为 {REDIRECT_FOUND} 且 actualReturnedCode 正常。
所以显然这里应该有异常,但是我无法理解错误消息的逻辑:
java.lang.AssertionError:
Expected: a collection containing <OK>
but: mismatches were: [was <REDIRECT_FOUND>]
如果是这样岂不是更好:
java.lang.AssertionError:
Expected: a status code one of <REDIRECT_FOUND>
but: mismatches were: [value was <OK>]
?
本以为会收到REDIRECT_FOUND但实际上我收到了OK!
如果我理解错了,你能解释一下吗?
assertThat()
方法的第一个参数指出,它必须是您获得的 "actual" 值:
assertThat
public static <T> void assertThat(T actual,
Matcher<? super T> matcher)
所以,你的actualReturnedCode
一定在前面。使用 isIn()
匹配来检查一个值是否在集合中:
assertThat(actualReturnedCode, isIn(expectedStatusCodesList));
虽然英语不是我的母语,但我仍然认为 Hamcrest 的以下错误信息是错误的。
我有以下验证方法:
private void validateReturnedCode() {
List<HTTPStatusCodes> expectedStatusCodesList = requestData.getExpectedHttpCodes().getStatusCodes();
HTTPStatusCodes actualReturnedCode = fromCode(response.getStatusCode());
assertThat(expectedStatusCodesList, hasItem(actualReturnedCode));
}
假设 expectedStatusCodesList 为 {REDIRECT_FOUND} 且 actualReturnedCode 正常。
所以显然这里应该有异常,但是我无法理解错误消息的逻辑:
java.lang.AssertionError:
Expected: a collection containing <OK>
but: mismatches were: [was <REDIRECT_FOUND>]
如果是这样岂不是更好:
java.lang.AssertionError:
Expected: a status code one of <REDIRECT_FOUND>
but: mismatches were: [value was <OK>]
?
本以为会收到REDIRECT_FOUND但实际上我收到了OK!
如果我理解错了,你能解释一下吗?
assertThat()
方法的第一个参数指出,它必须是您获得的 "actual" 值:
assertThat
public static <T> void assertThat(T actual, Matcher<? super T> matcher)
所以,你的actualReturnedCode
一定在前面。使用 isIn()
匹配来检查一个值是否在集合中:
assertThat(actualReturnedCode, isIn(expectedStatusCodesList));