Top Methods
org.assertj.core.api

public Interface AssertionErrorCollector

extends AfterAssertionErrorCollected
Known Direct Subinterfaces
org.assertj.core.api.SoftAssertionsProvider
Known Direct Implementers
org.assertj.core.api.DefaultAssertionErrorCollector, org.assertj.core.api.junit.jupiter.SoftAssertionsExtension.ThreadLocalErrorCollector
Imports
java.util.List, .Optional

Method Summary

Modifier and TypeMethod and Description
public List<AssertionError>
public void
collectAssertionError(AssertionError
the AssertionError to collect.
error
)

This method can be used to collect soft assertion errors.

public default Optional<AssertionErrorCollector>
public default void
onAssertionErrorCollected(AssertionError
the collected AssertionError.
assertionError
)

Implements org.assertj.core.api.AfterAssertionErrorCollected.onAssertionErrorCollected.

This method is called after each AssertionError is collected by soft assertions (you can't prevent the error to be collected).
public default void
setDelegate(AssertionErrorCollector
the AssertionErrorCollector to which the assertions will be forwarded.
delegate
)

Optionally sets a "delegate" collector into which the collected assertions will be deposited.

public void
succeeded()

Indicates/sets that the last assertion was a success.

public boolean

Returns:

true if the last assertion was a success.
wasSuccess
()

Returns the result of last soft assertion which can be used to decide what the next one should be.

Method Detail

assertionErrorsCollectedback to summary
public List<AssertionError> assertionErrorsCollected()
collectAssertionErrorback to summary
public void collectAssertionError(AssertionError error)

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.

getDelegateback to summary
public default Optional<AssertionErrorCollector> getDelegate()
onAssertionErrorCollectedback to summary
public default void onAssertionErrorCollected(AssertionError assertionError)

Implements org.assertj.core.api.AfterAssertionErrorCollected.onAssertionErrorCollected.

Doc from org.assertj.core.api.AfterAssertionErrorCollected.onAssertionErrorCollected.

This method is called after each AssertionError is collected by soft assertions (you can't prevent the error to be collected).

SoftAssertionsProvider provides a do-nothing implementation which you can override in your own custom soft assertions class.
Note that your custom soft assertions class must extend AbstractSoftAssertions as this is where the actual soft assertion errors collection is implemented.

If you just use the standard soft assertions classes provided by AssertJ, you can register your callback with setAfterAssertionErrorCollected(AfterAssertionErrorCollected) or addAfterAssertionErrorCollected(AfterAssertionErrorCollected) if you have many.

Example with custom soft assertions:

 class TolkienSoftAssertions extends SoftAssertions {

  public TolkienHeroesAssert assertThat(TolkienHero actual) {
    return proxy(TolkienHeroesAssert.class, TolkienHero.class, actual);
  }

  @Override
  public void onAssertionErrorCollected(AssertionError assertionError) {
    System.out.println(assertionError);
  }
}

TolkienSoftAssertions softly = new TolkienSoftAssertions();

TolkienCharacter frodo = TolkienCharacter.of("Frodo", 33, HOBBIT);

// the AssertionError corresponding to this failing assertion is printed to the console.
softly.assertThat(frodo).hasName("Bilbo");

Example with standard soft assertions:

 SoftAssertions softly = new SoftAssertions();

// register our callback
softly.addAfterAssertionErrorCollected(error -> System.out.println(error));

// the AssertionError corresponding to this failing assertion is printed to the console.
softly.assertThat("The Beatles").isEqualTo("The Rolling Stones");
Parameters
assertionError:AssertionError

the collected AssertionError.

Annotations
@Override
setDelegateback to summary
public default void setDelegate(AssertionErrorCollector delegate)

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.

succeededback to summary
public void succeeded()

Indicates/sets that the last assertion was a success.

wasSuccessback to summary
public boolean 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.