Skip to content

Commit f0545a5

Browse files
committed
Add throwsException matcher for throwable instance
1 parent 544a4cf commit f0545a5

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

hamcrest/src/main/java/org/hamcrest/Matchers.java

+12
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,18 @@ public static <T extends Runnable> Matcher<T> throwsException() {
22402240
return ThrowsException.throwsException();
22412241
}
22422242

2243+
/**
2244+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided <code>throwable</code>
2245+
*
2246+
* @param <U> type of the Runnable
2247+
* @param <T> type of the Throwable
2248+
* @param throwable the Throwable class against which examined exceptions are compared
2249+
* @return The matcher.
2250+
*/
2251+
public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(U throwable) {
2252+
return ThrowsException.throwsException(throwable);
2253+
}
2254+
22432255
/**
22442256
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class
22452257
*

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

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static <T extends Runnable> Matcher<T> throwsException() {
2626
return throwsException(Throwable.class);
2727
}
2828

29+
public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(U throwable) {
30+
return throwsException(throwable.getClass(), throwable.getMessage());
31+
}
32+
2933
public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(Class<U> throwableClass) {
3034
return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("<anything>"));
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Matchers of exceptions.
3+
*/
4+
package org.hamcrest.exception;

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import static org.hamcrest.MatcherAssert.assertThat;
66
import static org.hamcrest.Matchers.containsString;
7+
import static org.hamcrest.Matchers.throwsExceptionWithMessage;
78
import static org.hamcrest.exception.ThrowsException.throwsException;
89
import static org.hamcrest.test.MatcherAssertions.*;
910

@@ -22,7 +23,10 @@ public void examples() {
2223
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException());
2324
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class));
2425
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, "Boom!"));
26+
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(new IllegalArgumentException("Boom!")));
2527
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, containsString("Boo")));
28+
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage("Boom!"));
29+
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage(containsString("Boo")));
2630
}
2731

2832
@Test

0 commit comments

Comments
 (0)