Top Description Fields Constructors Methods
org.assertj.core.util

public final Class Throwables

extends Object
Class Inheritance
Static Imports
java.lang.String.format, java.util.Arrays.stream, .Collections.reverse, java.util.stream.Collectors.joining, .Collectors.toList, org.assertj.core.extractor.Extractors.byName, org.assertj.core.groups.FieldsOrPropertiesExtractor.extract, org.assertj.core.util.Lists.list, .Lists.newArrayList

Utility methods related to Throwables.
Authors
Alex Ruiz, Daniel Zlotin

Field Summary

Modifier and TypeField and Description
private static final Function<Throwable, String>
private static final String
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static <T extends Throwable> T
public static <T extends Throwable> List<T>
addLineNumberToErrorMessages(List<? extends T> errors)

public static void
appendStackTraceInCurrentThreadToThrowable(Throwable
the given Throwable.
t
,
String
the name of the method used as the starting point of the current thread's stack trace.
methodToStartFrom
)

Appends the stack trace of the current thread to the one in the given Throwable.

private static <T extends Throwable> T
private static String
buildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement)

private static <T extends Throwable> T
private static <T extends Throwable> T
public static List<String>
describeErrors(List<? extends Throwable> errors)

public static StackTraceElement
public static Throwable

Returns:

the root cause if any, else null.
getRootCause
(Throwable
the Throwable to get root cause from.
throwable
)

Get the root cause (i.e., the last non-null cause) from a Throwable.

public static String

Returns:

the stack trace as a String.
getStackTrace
(Throwable
the Throwable to get stack trace from.
throwable
)

Get the stack trace from a Throwable as a String.

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

private static <T extends Throwable> boolean
private static boolean
public static void
removeAssertJRelatedElementsFromStackTrace(Throwable
the Throwable to filter stack trace.
throwable
)

Removes the AssertJ-related elements from the Throwable stack trace that have little value for end user, that is assertj elements and the ones coming from assertj (for example assertj calling some java jdk classes to build assertion errors dynamically).

private static String
simpleClassNameOf(StackTraceElement testStackTraceElement)

private static List<StackTraceElement>
stackTraceInCurrentThread(String methodToStartFrom)

private static List<StackTraceElement>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ERROR_DESCRIPTION_EXTRACTORback to summary
private static final Function<Throwable, String> ERROR_DESCRIPTION_EXTRACTOR
JAVA_BASEback to summary
private static final String JAVA_BASE
JDK_BASEback to summary
private static final String JDK_BASE
ORG_ASSERTJback to summary
private static final String ORG_ASSERTJ

Constructor Detail

Throwablesback to summary
private Throwables()

Method Detail

addLineNumberToErrorMessageback to summary
private static <T extends Throwable> T addLineNumberToErrorMessage(T error)
addLineNumberToErrorMessagesback to summary
public static <T extends Throwable> List<T> addLineNumberToErrorMessages(List<? extends T> errors)
appendStackTraceInCurrentThreadToThrowableback to summary
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:Throwable

the given Throwable.

methodToStartFrom:String

the name of the method used as the starting point of the current thread's stack trace.

buildAssertionErrorWithLineNumbersButNoActualOrExpectedValuesback to summary
private static <T extends Throwable> T buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
buildErrorMessageWithLineNumberback to summary
private static String buildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement)
buildOpentest4jAssertionFailedErrorWithLineNumbersback to summary
private static <T extends Throwable> T buildOpentest4jAssertionFailedErrorWithLineNumbers(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
createNewInstanceWithLineNumberInErrorMessageback to summary
private static <T extends Throwable> T createNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
describeErrorsback to summary
public static List<String> describeErrors(List<? extends Throwable> errors)
getFirstStackTraceElementFromTestback to summary
public static StackTraceElement getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace)
getRootCauseback to summary
public static Throwable getRootCause(Throwable throwable)

Get the root cause (i.e., the last non-null cause) from a Throwable.

Parameters
throwable:Throwable

the Throwable to get root cause from.

Returns:Throwable

the root cause if any, else null.

getStackTraceback to summary
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:Throwable

the Throwable to get stack trace from.

Returns:String

the stack trace as a String.

isFromAssertJback to summary
private static boolean isFromAssertJ(StackTraceElement stackTrace)
isFromJavaOrJdkPackagesback to summary
private static boolean isFromJavaOrJdkPackages(StackTraceElement stackTrace)
isInstanceOfback to summary
private static boolean isInstanceOf(Object object, String className)
isOpentest4jAssertionFailedErrorback to summary
private static <T extends Throwable> boolean isOpentest4jAssertionFailedError(T error)
isProxiedAssertionClassback to summary
private static boolean isProxiedAssertionClass(String className)
removeAssertJRelatedElementsFromStackTraceback to summary
public static void removeAssertJRelatedElementsFromStackTrace(Throwable throwable)

Removes the AssertJ-related elements from the Throwable stack trace that have little value for end user, that is assertj elements and the ones coming from assertj (for example assertj calling some java jdk classes to build assertion errors dynamically).

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 examples.StackTraceFilterExample.main(StackTraceFilterExample.java:20)
Parameters
throwable:Throwable

the Throwable to filter stack trace.

simpleClassNameOfback to summary
private static String simpleClassNameOf(StackTraceElement testStackTraceElement)
stackTraceInCurrentThreadback to summary
private static List<StackTraceElement> stackTraceInCurrentThread(String methodToStartFrom)
stackTraceInCurrentThreadback to summary
private static List<StackTraceElement> stackTraceInCurrentThread()