Package org.assertj.core.util
Class Throwables
java.lang.Object
org.assertj.core.util.Throwables
Utility methods related to
Throwables.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T extends Throwable>
TaddLineNumberToErrorMessage(T error) addLineNumberToErrorMessages(List<? extends T> errors) static voidappendStackTraceInCurrentThreadToThrowable(Throwable t, String methodToStartFrom) Appends the stack trace of the current thread to the one in the given.Throwableprivate static <T extends Throwable>
TbuildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(T error, StackTraceElement testStackTraceElement) private static StringbuildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement) private static <T extends Throwable>
TbuildOpentest4jAssertionFailedErrorWithLineNumbers(T error, StackTraceElement testStackTraceElement) private static <T extends Throwable>
TcreateNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement) describeErrors(List<? extends Throwable> errors) static StackTraceElementgetFirstStackTraceElementFromTest(StackTraceElement[] stacktrace) static ThrowablegetRootCause(Throwable throwable) Get the root cause (i.e., the last non-null cause) from aThrowable.static StringgetStackTrace(Throwable throwable) private static booleanisFromAssertJ(StackTraceElement stackTrace) private static booleanisFromJavaOrJdkPackages(StackTraceElement stackTrace) private static booleanisInstanceOf(Object object, String className) private static <T extends Throwable>
booleanisOpentest4jAssertionFailedError(T error) private static booleanisProxiedAssertionClass(String className) static voidRemoves the AssertJ-related elements from thestack 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).Throwableprivate static StringsimpleClassNameOf(StackTraceElement testStackTraceElement) private static List<StackTraceElement> private static List<StackTraceElement> stackTraceInCurrentThread(String methodToStartFrom)
-
Field Details
-
ORG_ASSERTJ
- See Also:
-
JAVA_BASE
- See Also:
-
JDK_BASE
- See Also:
-
ERROR_DESCRIPTION_EXTRACTOR
-
-
Constructor Details
-
Throwables
private Throwables()
-
-
Method Details
-
describeErrors
-
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 givenThrowable.methodToStartFrom- the name of the method used as the starting point of the current thread's stack trace.
-
stackTraceInCurrentThread
-
stackTraceInCurrentThread
-
removeAssertJRelatedElementsFromStackTrace
Removes the AssertJ-related elements from thestack 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).ThrowableTherefore, instead of seeing this:
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 examples.StackTraceFilterExample.main(StackTraceFilterExample.java:20)- Parameters:
throwable- theThrowableto filter stack trace.
-
isFromAssertJ
-
isFromJavaOrJdkPackages
-
getRootCause
Get the root cause (i.e., the last non-null cause) from aThrowable.- Parameters:
throwable- theThrowableto get root cause from.- Returns:
- the root cause if any, else
null.
-
getStackTrace
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.
-
addLineNumberToErrorMessages
-
getFirstStackTraceElementFromTest
-
isProxiedAssertionClass
-
addLineNumberToErrorMessage
-
createNewInstanceWithLineNumberInErrorMessage
private static <T extends Throwable> T createNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException - Throws:
ReflectiveOperationException
-
isOpentest4jAssertionFailedError
-
isInstanceOf
-
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
-