Skip to content

Commit c72d23a

Browse files
authored
Merge pull request #2793 from anatolyuzhakov/feature/add-assert-throws-overloads
Added ability to provide custom message to assertThrows\expectThrows methods
2 parents 4de3c7b + 4d40e64 commit c72d23a

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
23
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs
34

45
7.6.1

testng-asserts/src/main/java/org/testng/Assert.java

+37-3
Original file line numberDiff line numberDiff line change
@@ -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
}

testng-asserts/src/test/java/test/asserttests/AssertTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,33 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
240240
fail();
241241
}
242242

243+
@Test
244+
public void expectThrowsWithErrorMessageProvidedByClientUponTypeMismatch() {
245+
NullPointerException npe = new NullPointerException();
246+
String messageProvidedByClient = "Client's message";
247+
248+
try {
249+
expectThrows(messageProvidedByClient, IOException.class, throwingRunnable(npe));
250+
} catch (AssertionError error) {
251+
assertEquals(messageProvidedByClient, error.getMessage());
252+
return;
253+
}
254+
fail();
255+
}
256+
257+
@Test
258+
public void expectThrowsWithErrorMessageProvidedByClientWhenNothingWasThrown() {
259+
String messageProvidedByClient = "Client's message";
260+
261+
try {
262+
expectThrows(messageProvidedByClient, Throwable.class, nonThrowingRunnable());
263+
} catch (AssertionError error) {
264+
assertEquals(messageProvidedByClient, error.getMessage());
265+
return;
266+
}
267+
fail();
268+
}
269+
243270
private static ThrowingRunnable nonThrowingRunnable() {
244271
return () -> {};
245272
}

0 commit comments

Comments
 (0)