@@ -2223,6 +2223,22 @@ public static <T extends Throwable> void assertThrows(
22232223 expectThrows (throwableClass , runnable );
22242224 }
22252225
2226+ /**
2227+ * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed.
2228+ * If it does not throw an exception, an {@link AssertionError} is thrown. If it throws the wrong
2229+ * type of exception, an {@code AssertionError} is thrown describing the mismatch; the exception
2230+ * that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
2231+ *
2232+ * @param message fail message
2233+ * @param throwableClass the expected type of the exception
2234+ * @param <T> the expected type of the exception
2235+ * @param runnable A function that is expected to throw an exception when invoked
2236+ */
2237+ public static <T extends Throwable > void assertThrows (
2238+ String message , Class <T > throwableClass , ThrowingRunnable runnable ) {
2239+ expectThrows (message , throwableClass , runnable );
2240+ }
2241+
22262242 /**
22272243 * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed
22282244 * and returns the exception. If {@code runnable} does not throw an exception, an {@link
@@ -2238,6 +2254,24 @@ public static <T extends Throwable> void assertThrows(
22382254 */
22392255 public static <T extends Throwable > T expectThrows (
22402256 Class <T > throwableClass , ThrowingRunnable runnable ) {
2257+ return expectThrows (null , throwableClass , runnable );
2258+ }
2259+
2260+ /**
2261+ * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed
2262+ * and returns the exception. If {@code runnable} does not throw an exception, an {@link
2263+ * AssertionError} is thrown. If it throws the wrong type of exception, an {@code AssertionError}
2264+ * is thrown describing the mismatch; the exception that was actually thrown can be obtained by
2265+ * calling {@link AssertionError#getCause}.
2266+ *
2267+ * @param message fail message
2268+ * @param throwableClass the expected type of the exception
2269+ * @param <T> the expected type of the exception
2270+ * @param runnable A function that is expected to throw an exception when invoked
2271+ * @return The exception thrown by {@code runnable}
2272+ */
2273+ public static <T extends Throwable > T expectThrows (
2274+ String message , Class <T > throwableClass , ThrowingRunnable runnable ) {
22412275 try {
22422276 runnable .run ();
22432277 } catch (Throwable t ) {
@@ -2249,12 +2283,12 @@ public static <T extends Throwable> T expectThrows(
22492283 "Expected %s to be thrown, but %s was thrown" ,
22502284 throwableClass .getSimpleName (), t .getClass ().getSimpleName ());
22512285
2252- throw new AssertionError (mismatchMessage , t );
2286+ throw new AssertionError (message != null ? message : mismatchMessage , t );
22532287 }
22542288 }
2255- String message =
2289+ String nothingThrownMessage =
22562290 String .format (
22572291 "Expected %s to be thrown, but nothing was thrown" , throwableClass .getSimpleName ());
2258- throw new AssertionError (message );
2292+ throw new AssertionError (message != null ? message : nothingThrownMessage );
22592293 }
22602294}
0 commit comments