Package org.assertj.core.api
Class DefaultAssertionErrorCollector
java.lang.Object
org.assertj.core.api.DefaultAssertionErrorCollector
- All Implemented Interfaces:
AfterAssertionErrorCollected,AssertionErrorCollector
- Direct Known Subclasses:
AbstractSoftAssertions
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) Register a callback allowing to react after anAssertionErroris collected by the current soft assertions.Returns a list of soft assertions collected errors.voidThis method can be used to collect soft assertion errors.decorateErrorsCollected(List<? extends T> errors) Modifies collected errors.voidsetAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) Same asaddAfterAssertionErrorCollected(AfterAssertionErrorCollected), but also removes all previously added callbacks.voidsetDelegate(AssertionErrorCollector delegate) Optionally sets a "delegate" collector into which the collected assertions will be deposited.voidIndicates/sets that the last assertion was a success.booleanReturns 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, waitMethods inherited from interface org.assertj.core.api.AssertionErrorCollector
onAssertionErrorCollected
-
Constructor Details
-
DefaultAssertionErrorCollector
public DefaultAssertionErrorCollector()
-
-
Method Details
-
setDelegate
Description copied from interface:AssertionErrorCollectorOptionally 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:
setDelegatein interfaceAssertionErrorCollector- Parameters:
delegate- theAssertionErrorCollectorto which the assertions will be forwarded.
-
getDelegate
- Specified by:
getDelegatein interfaceAssertionErrorCollector
-
collectAssertionError
Description copied from interface:AssertionErrorCollectorThis 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:
collectAssertionErrorin interfaceAssertionErrorCollector- Parameters:
error- theAssertionErrorto collect.
-
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:
assertionErrorsCollectedin interfaceAssertionErrorCollector- Returns:
- A list of soft assertions collected errors.
-
setAfterAssertionErrorCollected
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) Same asaddAfterAssertionErrorCollected(AfterAssertionErrorCollected), but also removes all previously added callbacks.- Parameters:
afterAssertionErrorCollected- the callback.- Since:
- 3.17.0
-
addAfterAssertionErrorCollected
public void addAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) Register a callback allowing to react after anAssertionErroris collected by the current soft assertions.The callback is an instance of
AfterAssertionErrorCollectedwhich can be expressed as a lambda.Example:
SoftAssertions softly = new SoftAssertions(); StringBuilder reportBuilder = new StringBuilder(String.format("Assertion errors report:%n")); // register a single callback with: softly.setAfterAssertionErrorCollected(error -> reportBuilder.append(String.format("------------------%n%s%n", error.getMessage()))); // or as many as needed with: // softly.addAfterAssertionErrorCollected(error -> reportBuilder.append(String.format("------------------%n%s%n", error.getMessage()))); // the AssertionErrors 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:Assertion errors 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.26.0
-
succeeded
public void succeeded()Description copied from interface:AssertionErrorCollectorIndicates/sets that the last assertion was a success.- Specified by:
succeededin interfaceAssertionErrorCollector
-
wasSuccess
public boolean wasSuccess()Description copied from interface:AssertionErrorCollectorReturns 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:
wasSuccessin interfaceAssertionErrorCollector- Returns:
- true if the last assertion was a success.
-
decorateErrorsCollected
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
-