Class Throwables

java.lang.Object
org.assertj.core.util.Throwables

public final class Throwables extends Object
Utility methods related to Throwables.
  • Field Details

  • Constructor Details

    • Throwables

      private Throwables()
  • Method Details

    • describeErrors

      public static List<String> describeErrors(List<? extends Throwable> errors)
    • appendStackTraceInCurrentThreadToThrowable

      public static void appendStackTraceInCurrentThreadToThrowable(Throwable t, String methodToStartFrom)
      Appends the stack trace of the current thread to the one in the given Throwable.
      Parameters:
      t - the given Throwable.
      methodToStartFrom - the name of the method used as the starting point of the current thread's stack trace.
    • stackTraceInCurrentThread

      private static List<StackTraceElement> stackTraceInCurrentThread(String methodToStartFrom)
    • stackTraceInCurrentThread

      private static List<StackTraceElement> stackTraceInCurrentThread()
    • removeAssertJRelatedElementsFromStackTrace

      public static void removeAssertJRelatedElementsFromStackTrace(Throwable throwable)
      Removes the AssertJ-related elements from the Throwable stack trace that have little value for end user. Therefore, instead of seeing this:
       org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
         at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
         at org.assertj.core.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34)
         at org.assertj.core.error.ShouldBeEqual.newComparisonFailure(ShouldBeEqual.java:111)
         at org.assertj.core.error.ShouldBeEqual.comparisonFailure(ShouldBeEqual.java:103)
         at org.assertj.core.error.ShouldBeEqual.newAssertionError(ShouldBeEqual.java:81)
         at org.assertj.core.internal.Failures.failure(Failures.java:76)
         at org.assertj.core.internal.Objects.assertEqual(Objects.java:116)
         at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:74)
         at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:13)
      We get this:
       org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
         at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:20)
      Parameters:
      throwable - the Throwable to filter stack trace.
    • getRootCause

      public static Throwable getRootCause(Throwable throwable)
      Get the root cause (ie the last non null cause) from a Throwable.
      Parameters:
      throwable - the Throwable to get root cause from.
      Returns:
      the root cause if any, else null.
    • getStackTrace

      public static String getStackTrace(Throwable throwable)
      Get the stack trace from a Throwable as a String.

      The result of this method vary by JDK version as this method uses Throwable.printStackTrace(java.io.PrintWriter). On JDK1.3 and earlier, the cause exception will not be shown unless the specified throwable alters printStackTrace.

      Parameters:
      throwable - the Throwable to get stack trace from.
      Returns:
      the stack trace as a String.
    • addLineNumberToErrorMessages

      public static <T extends Throwable> List<T> addLineNumberToErrorMessages(List<? extends T> errors)
    • getFirstStackTraceElementFromTest

      public static StackTraceElement getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace)
    • isProxiedAssertionClass

      private static boolean isProxiedAssertionClass(String className)
    • addLineNumberToErrorMessage

      private static <T extends Throwable> T addLineNumberToErrorMessage(T error)
    • createNewInstanceWithLineNumberInErrorMessage

      private static <T extends Throwable> T createNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • isOpentest4jAssertionFailedError

      private static <T extends Throwable> boolean isOpentest4jAssertionFailedError(T error)
    • isInstanceOf

      private static boolean isInstanceOf(Object object, String className)
    • buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues

      private static <T extends Throwable> T buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • buildOpentest4jAssertionFailedErrorWithLineNumbers

      private static <T extends Throwable> T buildOpentest4jAssertionFailedErrorWithLineNumbers(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • buildErrorMessageWithLineNumber

      private static String buildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement)
    • simpleClassNameOf

      private static String simpleClassNameOf(StackTraceElement testStackTraceElement)