From 4d40e64e9f2d1265b65bd809ee5ae589f6e8660e Mon Sep 17 00:00:00 2001 From: Anatolii Yuzhakov Date: Thu, 18 Aug 2022 00:07:02 +0300 Subject: [PATCH] - overloaded Assert.assertThrows and Assert.expectThrows methods; - added tests for overloaded Assert.expectThrows method. --- CHANGES.txt | 1 + .../src/main/java/org/testng/Assert.java | 40 +++++++++++++++++-- .../java/test/asserttests/AssertTest.java | 27 +++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index b237583662..577483cc4b 100644 --- a/CHANGES.txt +++ b/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 diff --git a/testng-asserts/src/main/java/org/testng/Assert.java b/testng-asserts/src/main/java/org/testng/Assert.java index f52a0df63f..239df244d3 100644 --- a/testng-asserts/src/main/java/org/testng/Assert.java +++ b/testng-asserts/src/main/java/org/testng/Assert.java @@ -2223,6 +2223,22 @@ public static 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 the expected type of the exception + * @param runnable A function that is expected to throw an exception when invoked + */ + public static void assertThrows( + String message, Class 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 @@ -2238,6 +2254,24 @@ public static void assertThrows( */ public static T expectThrows( Class 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 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 expectThrows( + String message, Class throwableClass, ThrowingRunnable runnable) { try { runnable.run(); } catch (Throwable t) { @@ -2249,12 +2283,12 @@ public static 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); } } diff --git a/testng-asserts/src/test/java/test/asserttests/AssertTest.java b/testng-asserts/src/test/java/test/asserttests/AssertTest.java index 070346f7bd..62ee70927d 100644 --- a/testng-asserts/src/test/java/test/asserttests/AssertTest.java +++ b/testng-asserts/src/test/java/test/asserttests/AssertTest.java @@ -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 () -> {}; }