Я пишу некоторые пользовательские совпадения, чтобы упростить утверждения junit. Большинство из них распространяется TypeSafeMatcher, так что мне нужно только переопределить три метода:Как проверить пользовательский сопоставитель hamcrest?
public class NoneConstraintViolationMatcher<T> extends
TypeSafeMatcher<Set<ConstraintViolation<T>>> {
@Override
public void describeTo(Description description) {
description.appendText("None constraint violations found");
}
@Override
protected void describeMismatchSafely(Set<ConstraintViolation<T>> item,
Description mismatchDescription) {
mismatchDescription.
appendText("Unexpected constraint violation found, but got ");
mismatchDescription.appendValueList("", ",", "", item);
}
@Override
protected boolean matchesSafely(Set<ConstraintViolation<T>> item) {
return item.isEmpty();
}
}
Мой вопрос заключается в том, чтобы проверить их? Мой текущий решение
public class NoneConstraintViolationMatcherUnitTests {
private NoneConstraintViolationMatcher<Object> matcher =
new NoneConstraintViolationMatcher<Object>();
@Test
public void returnsMatchedGivenNoneConstraintViolations() throws Excetpion {
assertTrue(matcher.matches(.....));
}
@Test
public void returnsMismatchedGivenSomeConstraintViolations() throws Excetpion {
assertThat(matcher.matches(.....), is(false));
}
@Test
public void returnsConstraintViolationsFoundWhenMismatched()
throws Exception {
StringBuilder out = new StringBuilder();
//I don't find anything could be used to assert in description
StringDescription description = new StringDescription(out);
matcher.describeMismatch(..someCvx, description);
assertThat(out.toString(),
equalTo("Unexpected constraint violation found, but got "));
}
}
Еще одно решение, которое приходит мне в голову написать тест JUnit и использовать @Rule ExpectedException (с handleAssertionError установлен верно).
Как вы, ребята, проверяете соответствия? Заранее спасибо.
Благодарим за отзыв. Легко ли тестировать описание с помощью этого решения? – Hippoom