From f0545a5189513ff2abdf226c061d18ac40e7510a Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Mon, 4 Nov 2024 10:17:02 +1100 Subject: [PATCH] Add throwsException matcher for throwable instance --- hamcrest/src/main/java/org/hamcrest/Matchers.java | 12 ++++++++++++ .../java/org/hamcrest/exception/ThrowsException.java | 4 ++++ .../java/org/hamcrest/exception/package-info.java | 4 ++++ .../org/hamcrest/exception/ThrowsExceptionTest.java | 4 ++++ 4 files changed, 24 insertions(+) create mode 100644 hamcrest/src/main/java/org/hamcrest/exception/package-info.java diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index eb71535b..714777c2 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -2240,6 +2240,18 @@ public static Matcher throwsException() { return ThrowsException.throwsException(); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided throwable + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwable the Throwable class against which examined exceptions are compared + * @return The matcher. + */ + public static Matcher throwsException(U throwable) { + return ThrowsException.throwsException(throwable); + } + /** * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class * diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 12255a32..91f5fec4 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -26,6 +26,10 @@ public static Matcher throwsException() { return throwsException(Throwable.class); } + public static Matcher throwsException(U throwable) { + return throwsException(throwable.getClass(), throwable.getMessage()); + } + public static Matcher throwsException(Class throwableClass) { return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("")); } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/package-info.java b/hamcrest/src/main/java/org/hamcrest/exception/package-info.java new file mode 100644 index 00000000..8b85543e --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/exception/package-info.java @@ -0,0 +1,4 @@ +/** + * Matchers of exceptions. + */ +package org.hamcrest.exception; diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java index 37445065..3fd3f51f 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java @@ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.throwsExceptionWithMessage; import static org.hamcrest.exception.ThrowsException.throwsException; import static org.hamcrest.test.MatcherAssertions.*; @@ -22,7 +23,10 @@ public void examples() { assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException()); assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class)); assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, "Boom!")); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(new IllegalArgumentException("Boom!"))); assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, containsString("Boo"))); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage("Boom!")); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage(containsString("Boo"))); } @Test