@@ -2223,6 +2223,22 @@ public static <T extends Throwable> void assertThrows(
2223
2223
expectThrows (throwableClass , runnable );
2224
2224
}
2225
2225
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
+
2226
2242
/**
2227
2243
* Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed
2228
2244
* 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(
2238
2254
*/
2239
2255
public static <T extends Throwable > T expectThrows (
2240
2256
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 ) {
2241
2275
try {
2242
2276
runnable .run ();
2243
2277
} catch (Throwable t ) {
@@ -2249,12 +2283,12 @@ public static <T extends Throwable> T expectThrows(
2249
2283
"Expected %s to be thrown, but %s was thrown" ,
2250
2284
throwableClass .getSimpleName (), t .getClass ().getSimpleName ());
2251
2285
2252
- throw new AssertionError (mismatchMessage , t );
2286
+ throw new AssertionError (message != null ? message : mismatchMessage , t );
2253
2287
}
2254
2288
}
2255
- String message =
2289
+ String nothingThrownMessage =
2256
2290
String .format (
2257
2291
"Expected %s to be thrown, but nothing was thrown" , throwableClass .getSimpleName ());
2258
- throw new AssertionError (message );
2292
+ throw new AssertionError (message != null ? message : nothingThrownMessage );
2259
2293
}
2260
2294
}
0 commit comments