Skip to content

Commit

Permalink
Merge pull request #2793 from anatolyuzhakov/feature/add-assert-throw…
Browse files Browse the repository at this point in the history
…s-overloads

Added ability to provide custom message to assertThrows\expectThrows methods
  • Loading branch information
juherr committed Aug 30, 2022
2 parents 4de3c7b + 4d40e64 commit c72d23a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -1,4 +1,5 @@
Current
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs

7.6.1
Expand Down
40 changes: 37 additions & 3 deletions testng-asserts/src/main/java/org/testng/Assert.java
Expand Up @@ -2223,6 +2223,22 @@ public static <T extends Throwable> void assertThrows(
expectThrows(throwableClass, runnable);
}

/**
* Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed.
* If it does not throw an exception, an {@link AssertionError} is thrown. If it throws the wrong
* type of exception, an {@code AssertionError} is thrown describing the mismatch; the exception
* that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
*
* @param message fail message
* @param throwableClass the expected type of the exception
* @param <T> the expected type of the exception
* @param runnable A function that is expected to throw an exception when invoked
*/
public static <T extends Throwable> void assertThrows(
String message, Class<T> throwableClass, ThrowingRunnable runnable) {
expectThrows(message, throwableClass, runnable);
}

/**
* Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed
* and returns the exception. If {@code runnable} does not throw an exception, an {@link
Expand All @@ -2238,6 +2254,24 @@ public static <T extends Throwable> void assertThrows(
*/
public static <T extends Throwable> T expectThrows(
Class<T> throwableClass, ThrowingRunnable runnable) {
return expectThrows(null, throwableClass, runnable);
}

/**
* Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed
* and returns the exception. If {@code runnable} does not throw an exception, an {@link
* AssertionError} is thrown. If it throws the wrong type of exception, an {@code AssertionError}
* is thrown describing the mismatch; the exception that was actually thrown can be obtained by
* calling {@link AssertionError#getCause}.
*
* @param message fail message
* @param throwableClass the expected type of the exception
* @param <T> the expected type of the exception
* @param runnable A function that is expected to throw an exception when invoked
* @return The exception thrown by {@code runnable}
*/
public static <T extends Throwable> T expectThrows(
String message, Class<T> throwableClass, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (Throwable t) {
Expand All @@ -2249,12 +2283,12 @@ public static <T extends Throwable> T expectThrows(
"Expected %s to be thrown, but %s was thrown",
throwableClass.getSimpleName(), t.getClass().getSimpleName());

throw new AssertionError(mismatchMessage, t);
throw new AssertionError(message != null ? message : mismatchMessage, t);
}
}
String message =
String nothingThrownMessage =
String.format(
"Expected %s to be thrown, but nothing was thrown", throwableClass.getSimpleName());
throw new AssertionError(message);
throw new AssertionError(message != null ? message : nothingThrownMessage);
}
}
27 changes: 27 additions & 0 deletions testng-asserts/src/test/java/test/asserttests/AssertTest.java
Expand Up @@ -240,6 +240,33 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
fail();
}

@Test
public void expectThrowsWithErrorMessageProvidedByClientUponTypeMismatch() {
NullPointerException npe = new NullPointerException();
String messageProvidedByClient = "Client's message";

try {
expectThrows(messageProvidedByClient, IOException.class, throwingRunnable(npe));
} catch (AssertionError error) {
assertEquals(messageProvidedByClient, error.getMessage());
return;
}
fail();
}

@Test
public void expectThrowsWithErrorMessageProvidedByClientWhenNothingWasThrown() {
String messageProvidedByClient = "Client's message";

try {
expectThrows(messageProvidedByClient, Throwable.class, nonThrowingRunnable());
} catch (AssertionError error) {
assertEquals(messageProvidedByClient, error.getMessage());
return;
}
fail();
}

private static ThrowingRunnable nonThrowingRunnable() {
return () -> {};
}
Expand Down

0 comments on commit c72d23a

Please sign in to comment.