Top Fields Constructors Methods
org.assertj.core.api

public Class DefaultAssertionErrorCollector

extends Object
implements AssertionErrorCollector
Class Inheritance
All Implemented Interfaces
org.assertj.core.api.AssertionErrorCollector, org.assertj.core.api.AfterAssertionErrorCollected
Known Direct Subclasses
org.assertj.core.api.AbstractSoftAssertions
Static Imports
java.util.Collections.synchronizedList, .Collections.unmodifiableList

Field Summary

Modifier and TypeField and Description
private final List<AfterAssertionErrorCollected>
private final List<AssertionError>
private AssertionErrorCollector
private volatile boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addAfterAssertionErrorCollected(AfterAssertionErrorCollected
the callback.
afterAssertionErrorCollected
)

Register a callback allowing to react after an AssertionError is collected by the current soft assertions.

public List<AssertionError>

Returns:

A list of soft assertions collected errors.
assertionErrorsCollected
()

Implements org.assertj.core.api.AssertionErrorCollector.assertionErrorsCollected.

Returns a list of soft assertions collected errors.
public void
collectAssertionError(AssertionError
the AssertionError to collect.
error
)

Implements org.assertj.core.api.AssertionErrorCollector.collectAssertionError.

This method can be used to collect soft assertion errors.
protected <
the supertype to use in the list return value
T extends Throwable
>
List<T>

Returns:

decorated list
decorateErrorsCollected
(List<? extends T>
list of errors to decorate
errors
)

Modifies collected errors.

public Optional<AssertionErrorCollector>
public void
setAfterAssertionErrorCollected(AfterAssertionErrorCollected
the callback.
afterAssertionErrorCollected
)

Same as DefaultAssertionErrorCollector#addAfterAssertionErrorCollected(AfterAssertionErrorCollected), but also removes all previously added callbacks.

public void
setDelegate(AssertionErrorCollector
the AssertionErrorCollector to which the assertions will be forwarded.
delegate
)

Overrides default org.assertj.core.api.AssertionErrorCollector.setDelegate.

Optionally sets a "delegate" collector into which the collected assertions will be deposited.
public void
succeeded()

Implements org.assertj.core.api.AssertionErrorCollector.succeeded.

Indicates/sets that the last assertion was a success.
public boolean
wasSuccess()

Implements org.assertj.core.api.AssertionErrorCollector.wasSuccess.

Returns the result of last soft assertion which can be used to decide what the next one should be.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callbacksback to summary
private final List<AfterAssertionErrorCollected> callbacks
collectedAssertionErrorsback to summary
private final List<AssertionError> collectedAssertionErrors
delegateback to summary
private AssertionErrorCollector delegate
wasSuccessback to summary
private volatile boolean wasSuccess

Constructor Detail

DefaultAssertionErrorCollectorback to summary
public DefaultAssertionErrorCollector()

Method Detail

addAfterAssertionErrorCollectedback to summary
public void addAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)

Register a callback allowing to react after an AssertionError is collected by the current soft assertions.

The callback is an instance of AfterAssertionErrorCollected which 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 override AfterAssertionErrorCollected#onAssertionErrorCollected(AssertionError).

Parameters
afterAssertionErrorCollected:AfterAssertionErrorCollected

the callback.

Since
3.26.0
assertionErrorsCollectedback to summary
public List<AssertionError> assertionErrorsCollected()

Implements org.assertj.core.api.AssertionErrorCollector.assertionErrorsCollected.

Returns a list of soft assertions collected errors. If a delegate has been set (see setDelegate(), then this method will return the result of the delegate's assertErrorsCollected().

Returns:List<AssertionError>

A list of soft assertions collected errors.

Annotations
@Override
collectAssertionErrorback to summary
public void collectAssertionError(AssertionError error)

Implements org.assertj.core.api.AssertionErrorCollector.collectAssertionError.

Doc from org.assertj.core.api.AssertionErrorCollector.collectAssertionError.

This method can be used to collect soft assertion errors.

To be able to react after an assertion error is collected, use onAssertionErrorCollected(AssertionError).

Parameters
error:AssertionError

the AssertionError to collect.

Annotations
@Override
decorateErrorsCollectedback to summary
protected <T extends Throwable> List<T> decorateErrorsCollected(List<? extends T> errors)

Modifies collected errors. Override to customize modification.

Parameters
<T>
the supertype to use in the list return value
errors:List<? extends T>

list of errors to decorate

Returns:List<T>

decorated list

getDelegateback to summary
public Optional<AssertionErrorCollector> getDelegate()

Overrides default org.assertj.core.api.AssertionErrorCollector.getDelegate.

Annotations
@Override
setAfterAssertionErrorCollectedback to summary
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)

Same as DefaultAssertionErrorCollector#addAfterAssertionErrorCollected(AfterAssertionErrorCollected), but also removes all previously added callbacks.

Parameters
afterAssertionErrorCollected:AfterAssertionErrorCollected

the callback.

Since
3.17.0
setDelegateback to summary
public void setDelegate(AssertionErrorCollector delegate)

Overrides default org.assertj.core.api.AssertionErrorCollector.setDelegate.

Doc from org.assertj.core.api.AssertionErrorCollector.setDelegate.

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.

Parameters
delegate:AssertionErrorCollector

the AssertionErrorCollector to which the assertions will be forwarded.

Annotations
@Override
succeededback to summary
public void succeeded()

Implements org.assertj.core.api.AssertionErrorCollector.succeeded.

Doc from org.assertj.core.api.AssertionErrorCollector.succeeded.

Indicates/sets that the last assertion was a success.

Annotations
@Override
wasSuccessback to summary
public boolean wasSuccess()

Implements org.assertj.core.api.AssertionErrorCollector.wasSuccess.

Doc from org.assertj.core.api.AssertionErrorCollector.wasSuccess.

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();
}
Returns:boolean

true if the last assertion was a success.

Annotations
@Override