Package org.assertj.core.util
Class Throwables
- java.lang.Object
-
- org.assertj.core.util.Throwables
-
public final class Throwables extends java.lang.ObjectUtility methods related tos.Throwable
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.function.Function<java.lang.Throwable,java.lang.String>ERROR_DESCRIPTION_EXTRACTORprivate static java.lang.StringJAVA_LANG_REFLECT_CONSTRUCTORprivate static java.lang.StringORG_ASSERTJprivate static java.lang.StringORG_ASSERTJ_CORE_ERROR_CONSTRUCTOR_INVOKER
-
Constructor Summary
Constructors Modifier Constructor Description privateThrowables()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappendStackTraceInCurrentThreadToThrowable(java.lang.Throwable t, java.lang.String methodToStartFrom)Appends the stack trace of the current thread to the one in the given.Throwablestatic java.util.List<java.lang.String>describeErrors(java.util.List<? extends java.lang.Throwable> errors)static java.lang.ThrowablegetRootCause(java.lang.Throwable throwable)Get the root cause (ie the last non null cause) from aThrowable.static java.lang.StringgetStackTrace(java.lang.Throwable throwable)Get the stack trace from aThrowableas aString.static voidremoveAssertJRelatedElementsFromStackTrace(java.lang.Throwable throwable)Removes the AssertJ-related elements from thestack trace that have little value for end user.Throwableprivate static java.util.List<java.lang.StackTraceElement>stackTraceInCurrentThread()private static java.util.List<java.lang.StackTraceElement>stackTraceInCurrentThread(java.lang.String methodToStartFrom)
-
-
-
Field Detail
-
ORG_ASSERTJ_CORE_ERROR_CONSTRUCTOR_INVOKER
private static final java.lang.String ORG_ASSERTJ_CORE_ERROR_CONSTRUCTOR_INVOKER
- See Also:
- Constant Field Values
-
JAVA_LANG_REFLECT_CONSTRUCTOR
private static final java.lang.String JAVA_LANG_REFLECT_CONSTRUCTOR
- See Also:
- Constant Field Values
-
ORG_ASSERTJ
private static final java.lang.String ORG_ASSERTJ
- See Also:
- Constant Field Values
-
ERROR_DESCRIPTION_EXTRACTOR
private static final java.util.function.Function<java.lang.Throwable,java.lang.String> ERROR_DESCRIPTION_EXTRACTOR
-
-
Method Detail
-
describeErrors
public static java.util.List<java.lang.String> describeErrors(java.util.List<? extends java.lang.Throwable> errors)
-
appendStackTraceInCurrentThreadToThrowable
public static void appendStackTraceInCurrentThreadToThrowable(java.lang.Throwable t, java.lang.String methodToStartFrom)Appends the stack trace of the current thread to the one in the given.Throwable- Parameters:
t- the givenThrowable.methodToStartFrom- the name of the method used as the starting point of the current thread's stack trace.
-
stackTraceInCurrentThread
private static java.util.List<java.lang.StackTraceElement> stackTraceInCurrentThread(java.lang.String methodToStartFrom)
-
stackTraceInCurrentThread
private static java.util.List<java.lang.StackTraceElement> stackTraceInCurrentThread()
-
removeAssertJRelatedElementsFromStackTrace
public static void removeAssertJRelatedElementsFromStackTrace(java.lang.Throwable throwable)
Removes the AssertJ-related elements from thestack trace that have little value for end user. Therefore, instead of seeing this:Throwable
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 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)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- theThrowableto filter stack trace.
-
getRootCause
public static java.lang.Throwable getRootCause(java.lang.Throwable throwable)
Get the root cause (ie the last non null cause) from aThrowable.- Parameters:
throwable- theThrowableto get root cause from.- Returns:
- the root cause if any, else
null.
-
getStackTrace
public static java.lang.String getStackTrace(java.lang.Throwable throwable)
Get the stack trace from aThrowableas aString.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- theThrowableto get stack trace from.- Returns:
- the stack trace as a
String.
-
-