Skip to content

Commit e0221d7

Browse files
committed
Change: support junit style, matching against type matching of exceptions.
At heart, the matcher keeps checking instances, but some QoL alias support matching against types.
1 parent d1da83d commit e0221d7

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.hamcrest.Matcher;
55
import org.hamcrest.TypeSafeMatcher;
66

7+
import static org.hamcrest.Matchers.allOf;
8+
import static org.hamcrest.Matchers.instanceOf;
79
import static org.hamcrest.exception.ThrowsExceptionEqualTo.exceptionEqualTo;
10+
import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage;
811

912
/**
1013
* Tests if a Runnable throws a matching exception.
@@ -25,6 +28,10 @@ public class ThrowsException<T extends Runnable> extends TypeSafeMatcher<T> {
2528
this.exceptionMatcher = elementMatcher;
2629
}
2730

31+
public static <U extends Runnable> ThrowsException<U> throwsException() {
32+
return new ThrowsException<>(instanceOf(Throwable.class));
33+
}
34+
2835
public static <U extends Runnable, T extends Throwable> ThrowsException<U> throwsException(T item) {
2936
return new ThrowsException<>(exceptionEqualTo(item));
3037
}
@@ -33,6 +40,18 @@ public static <U extends Runnable> ThrowsException<U> throwsException(Matcher<?
3340
return new ThrowsException<>(exceptionMatcher);
3441
}
3542

43+
public static <U extends Runnable, T extends Throwable> ThrowsException<U> throwsException(Class<T> item) {
44+
return new ThrowsException<>(instanceOf(item));
45+
}
46+
47+
public static <U extends Runnable, T extends Throwable> ThrowsException<U> throwsException(Class<T> item , String message) {
48+
return new ThrowsException<>(allOf(instanceOf(item), withMessage(message)));
49+
}
50+
51+
public static <U extends Runnable, T extends Throwable> ThrowsException<U> throwsException(Class<T> item , Matcher<? super Throwable> exceptionMatcher) {
52+
return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher));
53+
}
54+
3655
@Override
3756
public boolean matchesSafely(T runnable) {
3857
try {

hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
55

6+
import static org.hamcrest.MatcherAssert.assertThat;
67
import static org.hamcrest.Matchers.containsString;
78
import static org.hamcrest.Matchers.instanceOf;
89
import static org.hamcrest.exception.ThrowsException.throwsException;
@@ -16,7 +17,23 @@ private Runnable runnableThrowing(Throwable exception) {
1617

1718
@Override
1819
protected Matcher<?> createMatcher() {
19-
return throwsException(new IllegalArgumentException("Boom!"));
20+
return throwsException(IllegalArgumentException.class);
21+
}
22+
23+
public void testExamples() {
24+
RuntimeException boom = new RuntimeException("boom");
25+
Runnable codeThatThrows = () -> {
26+
throw boom;
27+
};
28+
29+
assertThat(codeThatThrows, throwsException());
30+
assertThat(codeThatThrows, throwsException(boom));
31+
assertThat(codeThatThrows, throwsException(RuntimeException.class));
32+
assertThat(codeThatThrows, throwsException(withMessage("boom")));
33+
assertThat(codeThatThrows, throwsException(withMessage(containsString("oom"))));
34+
assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom"));
35+
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom")));
36+
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom"))));
2037
}
2138

2239
public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() {
@@ -45,7 +62,8 @@ public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMess
4562
assertMismatchDescription(
4663
"the runnable didn't throw",
4764
throwsException(new IllegalArgumentException("Boom!")),
48-
(Runnable) () -> {}
65+
(Runnable) () -> {
66+
}
4967
);
5068
}
5169

0 commit comments

Comments
 (0)