Class Throwables

java.lang.Object
org.junit.internal.Throwables

public final class Throwables extends Object
Miscellaneous functions dealing with Throwable.
Since:
4.12
  • Field Details

    • getSuppressed

      private static final Method getSuppressed
    • TEST_FRAMEWORK_METHOD_NAME_PREFIXES

      private static final String[] TEST_FRAMEWORK_METHOD_NAME_PREFIXES
    • TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES

      private static final String[] TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES
    • REFLECTION_METHOD_NAME_PREFIXES

      private static final String[] REFLECTION_METHOD_NAME_PREFIXES
  • Constructor Details

    • Throwables

      private Throwables()
  • Method Details

    • rethrowAsException

      public static Exception rethrowAsException(Throwable e) throws Exception
      Rethrows the given Throwable, allowing the caller to declare that it throws Exception. This is useful when your callers have nothing reasonable they can do when a Throwable is thrown. This is declared to return Exception so it can be used in a throw clause:
       try {
         doSomething();
       } catch (Throwable e} {
         throw Throwables.rethrowAsException(e);
       }
       doSomethingLater();
       
      Parameters:
      e - exception to rethrow
      Returns:
      does not return anything
      Throws:
      Exception
      Since:
      4.12
    • rethrow

      private static <T extends Throwable> void rethrow(Throwable e) throws T
      Throws:
      T extends Throwable
    • getStacktrace

      public static String getStacktrace(Throwable exception)
      Returns the stacktrace of the given Throwable as a String.
      Since:
      4.13
    • getTrimmedStackTrace

      public static String getTrimmedStackTrace(Throwable exception)
      Gets a trimmed version of the stack trace of the given exception. Stack trace elements that are below the test method are filtered out.
      Returns:
      a trimmed stack trace, or the original trace if trimming wasn't possible
    • getTrimmedStackTraceLines

      private static List<String> getTrimmedStackTraceLines(Throwable exception)
    • initGetSuppressed

      private static Method initGetSuppressed()
    • hasSuppressed

      private static boolean hasSuppressed(Throwable exception)
    • getCauseStackTraceLines

      private static List<String> getCauseStackTraceLines(Throwable exception)
    • getFullStackTrace

      private static String getFullStackTrace(Throwable exception)
    • appendStackTraceLines

      private static void appendStackTraceLines(List<String> stackTraceLines, StringBuilder destBuilder)
    • asReversedList

      private static <T> List<T> asReversedList(List<T> list)
    • isTestFrameworkMethod

      private static boolean isTestFrameworkMethod(String methodName)
    • isReflectionMethod

      private static boolean isReflectionMethod(String methodName)
    • isMatchingMethod

      private static boolean isMatchingMethod(String methodName, String[] methodNamePrefixes)