Permalink
Browse files

Both hasCause() and hasMessage() describe mismatches

  • Loading branch information...
1 parent a37fd9a commit 5e9467c59b235ce84e7c7753c43609dea069e932 @marcphilipp marcphilipp committed Jul 20, 2012
@@ -9,6 +9,7 @@
import org.hamcrest.Description;
import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;
/**
@@ -92,9 +93,16 @@ public void describeTo(Description description) {
}
@Override
- public boolean matchesSafely(Throwable item) {
+ protected boolean matchesSafely(Throwable item) {
return matcher.matches(item.getMessage());
}
+
+ @Override
+ protected void describeMismatchSafely(Throwable item,
+ Description mismatchDescription) {
+ mismatchDescription.appendText("message ");
+ matcher.describeMismatch(item.getMessage(), mismatchDescription);
+ }
};
}
@@ -106,9 +114,16 @@ public void describeTo(Description description) {
}
@Override
- public boolean matchesSafely(Throwable item) {
+ protected boolean matchesSafely(Throwable item) {
return matcher.matches(item.getCause());
}
+
+ @Override
+ protected void describeMismatchSafely(Throwable item,
+ Description mismatchDescription) {
+ mismatchDescription.appendText("cause ");
+ matcher.describeMismatch(item.getCause(), mismatchDescription);
+ }
};
}
}
@@ -47,7 +47,7 @@
{
HasWrongMessage.class,
hasSingleFailureWithMessage(startsWith("\nExpected: exception with message a string containing \"expectedMessage\"\n"
- + " but: was <java.lang.IllegalArgumentException: actualMessage>")) },
+ + " but: message was \"actualMessage\"")) },
{
ThrowNoExceptionButExpectExceptionWithType.class,
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.NullPointerException") },
@@ -86,6 +86,7 @@
hasSingleFailureWithMessage(CoreMatchers.<String>allOf(
startsWith("\nExpected: ("),
containsString("exception with cause is <java.lang.NullPointerException: expected cause>"),
+ containsString("cause was <java.lang.NullPointerException: an unexpected cause>"),
containsString("Stacktrace was: java.lang.IllegalArgumentException: Ack!"),
containsString("Caused by: java.lang.NullPointerException: an unexpected cause"))) }
});

0 comments on commit 5e9467c

Please sign in to comment.