Created
May 22, 2017 17:59
-
-
Save compwron/fb94853badbadc9b6605b991202f79fe to your computer and use it in GitHub Desktop.
error matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Matcher<ErrorCollector> containsErrorCode(final ErrorCode errorCode) { | |
return new BaseMatcher<ErrorCollector>() { | |
@Override | |
public boolean matches(final Object item) { | |
ErrorCollector allErrors = (ErrorCollector) item; | |
FieldError fieldError = allErrors.getFieldError(errorCode.getField()); | |
if (fieldError == null) { | |
return false; | |
} | |
if (!fieldError.getCode().equals(errorCode.getCode())) { | |
return false; | |
} | |
if (!fieldError.getDefaultMessage().equals(errorCode.getMessage())) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public void describeMismatch(Object item, Description mismatchDescription) { | |
ErrorCollector allErrors = (ErrorCollector) item; | |
String errorList = String.join(", ", allErrors.getFieldErrors() | |
.stream() | |
.map(FieldError::getField) | |
.collect(Collectors.toList())); | |
mismatchDescription.appendText("Error keys were: ").appendText(errorList); | |
} | |
@Override | |
public void describeTo(final Description description) { | |
description.appendText("Errors should include ").appendValue(errorCode); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment