나는 junit을 단언하기 위해 몇 가지 맞춤 matcher를 쓰고있다. 그들 중 대부분은 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 true로 설정하여) 사용이다.
당신은 어떻게 matcher를 테스트합니까? 미리 감사드립니다.
응답 해 주셔서 감사합니다. 이 솔루션을 사용하여 설명을 쉽게 테스트 할 수 있습니까? – Hippoom