Skip to content

Commit 94e0c1e

Browse files
committed
Change: Port Throwable matchers to TypeSafeDiagnosingMatcher
1 parent f9101be commit 94e0c1e

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.hamcrest.exception;
22

33
import org.hamcrest.Description;
4-
import org.hamcrest.TypeSafeMatcher;
4+
import org.hamcrest.TypeSafeDiagnosingMatcher;
55

66
/**
77
* A matcher that checks if a Runnable throws an expected exception when run.
88
*
99
* @param <T> the type of the expected exception.
1010
*/
11-
public class ThrowsExceptionEqualTo<T extends Throwable> extends TypeSafeMatcher<T> {
11+
public class ThrowsExceptionEqualTo<T extends Throwable> extends TypeSafeDiagnosingMatcher<T> {
1212
private final T expectedException;
1313

1414
/**
@@ -25,21 +25,21 @@ public static <U extends Throwable> ThrowsExceptionEqualTo<U> exceptionEqualTo(U
2525
}
2626

2727
@Override
28-
public boolean matchesSafely(T item) {
29-
return this.expectedException.getClass().isAssignableFrom(item.getClass()) && item.getMessage().equals(this.expectedException.getMessage());
28+
public boolean matchesSafely(T item, Description mismatchDescription) {
29+
if (!this.expectedException.getClass().isAssignableFrom(item.getClass())) {
30+
mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception");
31+
return false;
32+
}
33+
if (!item.getMessage().equals(this.expectedException.getMessage())) {
34+
mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage());
35+
return false;
36+
}
37+
38+
return true;
3039
}
3140

3241
@Override
3342
public void describeTo(Description description) {
3443
description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage());
3544
}
36-
37-
@Override
38-
protected void describeMismatchSafely(T item, Description mismatchDescription) {
39-
if (this.expectedException.getClass().isAssignableFrom(item.getClass())) {
40-
mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage());
41-
return;
42-
}
43-
mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception");
44-
}
4545
}

hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5-
import org.hamcrest.TypeSafeMatcher;
5+
import org.hamcrest.TypeSafeDiagnosingMatcher;
66

77
import static org.hamcrest.core.IsEqual.equalTo;
88

@@ -11,7 +11,7 @@
1111
*
1212
* @param <T> the type of the expected exception.
1313
*/
14-
public class ThrowsExceptionWithMessage<T extends Throwable> extends TypeSafeMatcher<T> {
14+
public class ThrowsExceptionWithMessage<T extends Throwable> extends TypeSafeDiagnosingMatcher<T> {
1515
private final Matcher<? super String> messageMatcher;
1616

1717
/**
@@ -32,17 +32,17 @@ public static <U extends Throwable> ThrowsExceptionWithMessage<U> withMessage(Ma
3232
}
3333

3434
@Override
35-
protected boolean matchesSafely(T item) {
36-
return this.messageMatcher.matches(item.getMessage());
35+
protected boolean matchesSafely(T item, Description mismatchDescription) {
36+
if (!this.messageMatcher.matches(item.getMessage())) {
37+
mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher);
38+
return false;
39+
}
40+
41+
return true;
3742
}
3843

3944
@Override
4045
public void describeTo(Description description) {
4146
description.appendText("an exception with message ").appendDescriptionOf(messageMatcher);
4247
}
43-
44-
@Override
45-
protected void describeMismatchSafely(T item, Description mismatchDescription) {
46-
mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher);
47-
}
4848
}

0 commit comments

Comments
 (0)