[testng] 84/160: Add expectThrows

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:22:24 UTC 2015


This is an automated email from the git hooks/post-receive script.

eugene-guest pushed a commit to annotated tag testng-6.9.5
in repository testng.

commit 36b6a2b5b43b8ff5adf7fe43acb5b41dd7049d94
Author: Ryan Schmitt <rschmitt at pobox.com>
Date:   Thu Jun 4 23:47:21 2015 -0700

    Add expectThrows
---
 src/main/java/org/testng/Assert.java           | 50 ++++++++++++++++
 src/test/java/test/asserttests/AssertTest.java | 82 ++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)

diff --git a/src/main/java/org/testng/Assert.java b/src/main/java/org/testng/Assert.java
index b004202..e56ba3f 100644
--- a/src/main/java/org/testng/Assert.java
+++ b/src/main/java/org/testng/Assert.java
@@ -952,4 +952,54 @@ public class Assert {
   static public void assertNotEquals(double actual1, double actual2, double delta) {
     assertNotEquals(actual1, actual2, delta, null);
   }
+
+  /**
+   * This interface facilitates the use of {@link #expectThrows} from Java 8. It allows
+   * method references to both void and non-void methods to be passed directly into
+   * expectThrows without wrapping, even if they declare checked exceptions.
+   * <p/>
+   * This interface is not meant to be implemented directly.
+   */
+  public interface ThrowingRunnable {
+    void run() throws Throwable;
+  }
+
+  /**
+   * Asserts that {@code runnable} throws an exception when invoked. If it does not, an
+   * {@link AssertionError} is thrown.
+   *
+   * @param runnable A function that is expected to throw an exception when invoked
+   */
+  public static void assertThrows(ThrowingRunnable runnable) {
+    expectThrows(Throwable.class, runnable);
+  }
+
+  /**
+   * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when
+   * executed. If it does, the exception object is returned. 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 throwableClass 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(Class<T> throwableClass, ThrowingRunnable runnable) {
+    try {
+      runnable.run();
+    } catch (Throwable t) {
+      if (throwableClass.isInstance(t)) {
+        return throwableClass.cast(t);
+      } else {
+        String mismatchMessage = String.format("Expected %s to be thrown, but %s was thrown",
+                throwableClass.getSimpleName(), t.getClass().getSimpleName());
+
+        throw new AssertionError(mismatchMessage, t);
+      }
+    }
+    String message = String.format("Expected %s to be thrown, but nothing was thrown",
+            throwableClass.getSimpleName());
+    throw new AssertionError(message);
+  }
 }
diff --git a/src/test/java/test/asserttests/AssertTest.java b/src/test/java/test/asserttests/AssertTest.java
index 054442d..f935493 100644
--- a/src/test/java/test/asserttests/AssertTest.java
+++ b/src/test/java/test/asserttests/AssertTest.java
@@ -1,11 +1,18 @@
 package test.asserttests;
 
 import org.testng.Assert;
+import org.testng.Assert.ThrowingRunnable;
 import org.testng.annotations.Test;
 import org.testng.collections.Sets;
 
+import java.io.IOException;
 import java.util.Set;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.testng.Assert.expectThrows;
+
 public class AssertTest {
 
   @Test
@@ -74,4 +81,79 @@ public class AssertTest {
     Assert.assertEquals(set2, set1);
   }
 
+  @Test(expectedExceptions = AssertionError.class)
+  public void expectThrowsRequiresAnExceptionToBeThrown() {
+    expectThrows(Throwable.class, nonThrowingRunnable());
+  }
+
+  @Test
+  public void expectThrowsIncludesAnInformativeDefaultMessage() {
+    try {
+      expectThrows(Throwable.class, nonThrowingRunnable());
+    } catch (AssertionError ex) {
+      assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage());
+      return;
+    }
+    fail();
+  }
+
+  @Test
+  public void expectThrowsReturnsTheSameObjectThrown() {
+    NullPointerException npe = new NullPointerException();
+
+    Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe));
+
+    assertSame(npe, throwable);
+  }
+
+  @Test(expectedExceptions = AssertionError.class)
+  public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
+    NullPointerException npe = new NullPointerException();
+
+    expectThrows(IOException.class, throwingRunnable(npe));
+  }
+
+  @Test
+  public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
+    NullPointerException npe = new NullPointerException("inner-message");
+
+    try {
+      expectThrows(IOException.class, throwingRunnable(npe));
+    } catch (AssertionError ex) {
+      assertSame(npe, ex.getCause());
+      assertEquals("inner-message", ex.getCause().getMessage());
+      return;
+    }
+    fail();
+  }
+
+  @Test
+  public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
+    NullPointerException npe = new NullPointerException();
+
+    try {
+      expectThrows(IOException.class, throwingRunnable(npe));
+    } catch (AssertionError error) {
+      assertEquals("Expected IOException to be thrown, but NullPointerException was thrown",
+              error.getMessage());
+      assertSame(npe, error.getCause());
+      return;
+    }
+    fail();
+  }
+
+  private static ThrowingRunnable nonThrowingRunnable() {
+    return new ThrowingRunnable() {
+      public void run() throws Throwable {
+      }
+    };
+  }
+
+  private static ThrowingRunnable throwingRunnable(final Throwable t) {
+    return new ThrowingRunnable() {
+      public void run() throws Throwable {
+        throw t;
+      }
+    };
+  }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git



More information about the pkg-java-commits mailing list