Package org.assertj.core.api
Class DefaultAssertionErrorCollector
- java.lang.Object
-
- org.assertj.core.api.DefaultAssertionErrorCollector
-
- All Implemented Interfaces:
AfterAssertionErrorCollected
,AssertionErrorCollector
- Direct Known Subclasses:
AbstractSoftAssertions
public class DefaultAssertionErrorCollector extends Object implements AssertionErrorCollector
-
-
Field Summary
Fields Modifier and Type Field Description private AfterAssertionErrorCollected
callback
private List<AssertionError>
collectedAssertionErrors
private AssertionErrorCollector
delegate
private boolean
wasSuccess
-
Constructor Summary
Constructors Constructor Description DefaultAssertionErrorCollector()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <T extends Throwable>
TaddLineNumberToErrorMessage(T error)
private static <T extends Throwable>
List<T>addLineNumberToErrorMessages(List<? extends T> errors)
List<AssertionError>
assertionErrorsCollected()
Returns a list of soft assertions collected errors.private static <T extends Throwable>
TbuildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(T error, StackTraceElement testStackTraceElement)
private static String
buildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement)
private static <T extends Throwable>
TbuildOpentest4jAssertionFailedErrorWithLineNumbers(T error, StackTraceElement testStackTraceElement)
void
collectAssertionError(AssertionError error)
This method can be used to collect soft assertion errors.private static <T extends Throwable>
TcreateNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement)
protected <T extends Throwable>
List<T>decorateErrorsCollected(List<? extends T> errors)
Modifies collected errors.Optional<AssertionErrorCollector>
getDelegate()
private static StackTraceElement
getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace)
private static <T extends Throwable>
booleanisOpentest4jAssertionFailedError(T error)
private static boolean
isProxiedAssertionClass(String className)
void
setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)
Register a callback allowing to react after anAssertionError
is collected by the current soft assertion.void
setDelegate(AssertionErrorCollector delegate)
Optionally sets a "delegate" collector into which the collected assertions will be deposited.private static String
simpleClassNameOf(StackTraceElement testStackTraceElement)
void
succeeded()
Indicates/sets that the last assertion was a success.boolean
wasSuccess()
Returns the result of last soft assertion which can be used to decide what the next one should be.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.AssertionErrorCollector
onAssertionErrorCollected
-
-
-
-
Field Detail
-
wasSuccess
private volatile boolean wasSuccess
-
collectedAssertionErrors
private List<AssertionError> collectedAssertionErrors
-
callback
private AfterAssertionErrorCollected callback
-
delegate
private AssertionErrorCollector delegate
-
-
Method Detail
-
setDelegate
public void setDelegate(AssertionErrorCollector delegate)
Description copied from interface:AssertionErrorCollector
Optionally sets a "delegate" collector into which the collected assertions will be deposited.Note that if you set a delegate, this instance will no longer collect or report assertion errors itself but will forward them all to the delegate for collection.
- Specified by:
setDelegate
in interfaceAssertionErrorCollector
- Parameters:
delegate
- theAssertionErrorCollector
to which the assertions will be forwarded.
-
getDelegate
public Optional<AssertionErrorCollector> getDelegate()
- Specified by:
getDelegate
in interfaceAssertionErrorCollector
-
collectAssertionError
public void collectAssertionError(AssertionError error)
Description copied from interface:AssertionErrorCollector
This method can be used to collect soft assertion errors.To be able to react after an assertion error is collected, use
AssertionErrorCollector.onAssertionErrorCollected(AssertionError)
.- Specified by:
collectAssertionError
in interfaceAssertionErrorCollector
- Parameters:
error
- theAssertionError
to collect.
-
assertionErrorsCollected
public List<AssertionError> assertionErrorsCollected()
Returns a list of soft assertions collected errors. If a delegate has been set (seesetDelegate()
, then this method will return the result of the delegate'sassertErrorsCollected()
.- Specified by:
assertionErrorsCollected
in interfaceAssertionErrorCollector
- Returns:
- A list of soft assertions collected errors.
-
setAfterAssertionErrorCollected
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)
Register a callback allowing to react after anAssertionError
is collected by the current soft assertion.The callback is an instance of
AfterAssertionErrorCollected
which can be expressed as lambda.Example:
SoftAssertions softly = new SoftAssertions(); StringBuilder reportBuilder = new StringBuilder(format("Assertions report:%n")); // register our callback softly.setAfterAssertionErrorCollected(error -> reportBuilder.append(String.format("------------------%n%s%n", error.getMessage()))); // the AssertionError corresponding to the failing assertions are registered in the report softly.assertThat("The Beatles").isEqualTo("The Rolling Stones"); softly.assertThat(123).isEqualTo(123) .isEqualTo(456);
resulting
reportBuilder
:Assertions report: ------------------ Expecting: <"The Beatles"> to be equal to: <"The Rolling Stones"> but was not. ------------------ Expecting: <123> to be equal to: <456> but was not.
Alternatively, if you have defined your own SoftAssertions subclass and inherited from
AbstractSoftAssertions
, the only thing you have to do is to overrideAfterAssertionErrorCollected.onAssertionErrorCollected(AssertionError)
.- Parameters:
afterAssertionErrorCollected
- the callback.- Since:
- 3.17.0
-
succeeded
public void succeeded()
Description copied from interface:AssertionErrorCollector
Indicates/sets that the last assertion was a success.- Specified by:
succeeded
in interfaceAssertionErrorCollector
-
wasSuccess
public boolean wasSuccess()
Description copied from interface:AssertionErrorCollector
Returns the result of last soft assertion which can be used to decide what the next one should be.Example:
Person person = ... SoftAssertions soft = new SoftAssertions(); if (soft.assertThat(person.getAddress()).isNotNull().wasSuccess()) { soft.assertThat(person.getAddress().getStreet()).isNotNull(); }
- Specified by:
wasSuccess
in interfaceAssertionErrorCollector
- Returns:
- true if the last assertion was a success.
-
decorateErrorsCollected
protected <T extends Throwable> List<T> decorateErrorsCollected(List<? extends T> errors)
Modifies collected errors. Override to customize modification.- Type Parameters:
T
- the supertype to use in the list return value- Parameters:
errors
- list of errors to decorate- Returns:
- decorated list
-
addLineNumberToErrorMessages
private static <T extends Throwable> List<T> addLineNumberToErrorMessages(List<? extends T> errors)
-
addLineNumberToErrorMessage
private static <T extends Throwable> T addLineNumberToErrorMessage(T error)
-
createNewInstanceWithLineNumberInErrorMessage
private static <T extends Throwable> T createNewInstanceWithLineNumberInErrorMessage(T error, StackTraceElement testStackTraceElement) throws ReflectiveOperationException
- Throws:
ReflectiveOperationException
-
isOpentest4jAssertionFailedError
private static <T extends Throwable> boolean isOpentest4jAssertionFailedError(T error)
-
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
private static String simpleClassNameOf(StackTraceElement testStackTraceElement)
-
getFirstStackTraceElementFromTest
private static StackTraceElement getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace)
-
isProxiedAssertionClass
private static boolean isProxiedAssertionClass(String className)
-
-