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

public Class Failures

extends Object
Class Inheritance
Static Imports
java.lang.String.format, org.assertj.core.util.Strings.isNullOrEmpty

Failure actions.
Authors
Yvonne Wang, Alex Ruiz

Field Summary

Modifier and TypeField and Description
private AssertionErrorCreator
private static final Failures
private static final String
private boolean
printThreadDump

flag indicating that in case of a failure a threaddump is printed out.

private boolean
removeAssertJRelatedElementsFromStackTrace

flag indicating whether or not we remove elements related to AssertJ from assertion error stack trace.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected String
public void
enablePrintThreadDump()

Set the flag indicating that in case of a failure a threaddump is printed out.

public AssertionError

Returns:

the created AssertionError.
expectedThrowableNotThrown
(Class<? extends Throwable>
the Throwable class that was expected to be thrown.
throwableClass
)

Creates a AssertionError for a Throwable class that was expected to be thrown.

public AssertionError

Returns:

the created AssertionError.
failure
(AssertionInfo
contains information about the failed assertion.
info
,
AssertionErrorFactory
knows how to create AssertionErrors.
factory
)

Deprecated as a consequence of AssertionErrorFactory being deprecated.
Creates a AssertionError following this pattern:
  1. creates a AssertionError using AssertionInfo#overridingErrorMessage() as the error message if such value is not null, or
  2. uses the given AssertionErrorFactory to create an AssertionError, prepending the value of AssertionInfo#description() to the error message
public AssertionError

Returns:

the created AssertionError.
failure
(AssertionInfo
contains information about the failed assertion.
info
,
ErrorMessageFactory
knows how to create detail messages for AssertionErrors.
messageFactory
)

Creates a AssertionError following this pattern:

  1. creates a AssertionError using AssertionInfo#overridingErrorMessage() as the error message if such value is not null, or
  2. uses the given ErrorMessageFactory to create the detail message of the AssertionError, prepending the value of AssertionInfo#description() to the error message
public AssertionError
failure(AssertionInfo info, ErrorMessageFactory messageFactory, Object actual, Object expected)

public AssertionError

Returns:

the created AssertionError.
failure
(String
the message of the AssertionError to create.
message
)

Creates a AssertionError using the given String as message.

public AssertionError
public static Failures

Returns:

the singleton instance of this class.
instance
()

Returns the singleton instance of this class.

public boolean

Returns:

whether or not we remove elements related to AssertJ from assertion error stack trace.
isRemoveAssertJRelatedElementsFromStackTrace
()

Returns whether or not we remove elements related to AssertJ from assertion error stack trace.

public void
public void
removeAssertJRelatedElementsFromStackTraceIfNeeded(AssertionError
the AssertionError to filter stack trace if option is set.
assertionError
)

If is removeAssertJRelatedElementsFromStackTrace is true, it filters the stack trace of the given AssertionError by removing stack trace elements related to AssertJ in order to get a more readable stack trace.

public void
setRemoveAssertJRelatedElementsFromStackTrace(boolean
flag
removeAssertJRelatedElementsFromStackTrace
)

Sets whether we remove elements related to AssertJ from assertion error stack trace.

public static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

assertionErrorCreatorback to summary
private AssertionErrorCreator assertionErrorCreator
INSTANCEback to summary
private static final Failures INSTANCE
LINE_SEPARATORback to summary
private static final String LINE_SEPARATOR
printThreadDumpback to summary
private boolean printThreadDump

flag indicating that in case of a failure a threaddump is printed out.

removeAssertJRelatedElementsFromStackTraceback to summary
private boolean removeAssertJRelatedElementsFromStackTrace

flag indicating whether or not we remove elements related to AssertJ from assertion error stack trace.

Constructor Detail

Failuresback to summary
pack-priv Failures()
Annotations
@VisibleForTesting

Method Detail

assertionErrorMessageback to summary
protected String assertionErrorMessage(AssertionInfo info, ErrorMessageFactory messageFactory)
enablePrintThreadDumpback to summary
public void enablePrintThreadDump()

Set the flag indicating that in case of a failure a threaddump is printed out.

expectedThrowableNotThrownback to summary
public AssertionError expectedThrowableNotThrown(Class<? extends Throwable> throwableClass)

Creates a AssertionError for a Throwable class that was expected to be thrown.

Parameters
throwableClass:Class<? extends Throwable>

the Throwable class that was expected to be thrown.

Returns:AssertionError

the created AssertionError.

Since
2.6.0 / 3.6.0
failureback to summary
public AssertionError failure(AssertionInfo info, AssertionErrorFactory factory)

Deprecated

as a consequence of AssertionErrorFactory being deprecated. See corresponding docs for further information.

Creates a AssertionError following this pattern:

  1. creates a AssertionError using AssertionInfo#overridingErrorMessage() as the error message if such value is not null, or
  2. uses the given AssertionErrorFactory to create an AssertionError, prepending the value of AssertionInfo#description() to the error message
Parameters
info:AssertionInfo

contains information about the failed assertion.

factory:AssertionErrorFactory

knows how to create AssertionErrors.

Returns:AssertionError

the created AssertionError.

failureback to summary
public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory)

Creates a AssertionError following this pattern:

  1. creates a AssertionError using AssertionInfo#overridingErrorMessage() as the error message if such value is not null, or
  2. uses the given ErrorMessageFactory to create the detail message of the AssertionError, prepending the value of AssertionInfo#description() to the error message
Parameters
info:AssertionInfo

contains information about the failed assertion.

messageFactory:ErrorMessageFactory

knows how to create detail messages for AssertionErrors.

Returns:AssertionError

the created AssertionError.

failureback to summary
public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory, Object actual, Object expected)
failureback to summary
public AssertionError failure(String message)

Creates a AssertionError using the given String as message.

It filters the AssertionError stack trace by default, to have full stack trace use setRemoveAssertJRelatedElementsFromStackTrace(boolean).

Parameters
message:String

the message of the AssertionError to create.

Returns:AssertionError

the created AssertionError.

failureIfErrorMessageIsOverriddenback to summary
public AssertionError failureIfErrorMessageIsOverridden(AssertionInfo info)
instanceback to summary
public static Failures instance()

Returns the singleton instance of this class.

Returns:Failures

the singleton instance of this class.

isRemoveAssertJRelatedElementsFromStackTraceback to summary
public boolean isRemoveAssertJRelatedElementsFromStackTrace()

Returns whether or not we remove elements related to AssertJ from assertion error stack trace.

Returns:boolean

whether or not we remove elements related to AssertJ from assertion error stack trace.

printThreadDumpIfNeededback to summary
public void printThreadDumpIfNeeded()
removeAssertJRelatedElementsFromStackTraceIfNeededback to summary
public void removeAssertJRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError)

If is removeAssertJRelatedElementsFromStackTrace is true, it filters the stack trace of the given AssertionError by removing stack trace elements related to AssertJ in order to get a more readable stack trace.

See example below :

 --------------- stack trace not filtered -----------------
 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)

 --------------- stack trace filtered -----------------
 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)
Method is public because we need to call it from ShouldBeEqual#newAssertionError(Description, org.assertj.core.presentation.Representation) that is building a junit ComparisonFailure by reflection.
Parameters
assertionError:AssertionError

the AssertionError to filter stack trace if option is set.

setRemoveAssertJRelatedElementsFromStackTraceback to summary
public void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace)

Sets whether we remove elements related to AssertJ from assertion error stack trace.

Parameters
removeAssertJRelatedElementsFromStackTrace:boolean

flag

threadDumpDescriptionback to summary
public static String threadDumpDescription()