Skip to content

Commit

Permalink
SoftAssertions callbacks is ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
Achitheus committed Feb 5, 2024
1 parent 13b8a7b commit a5f9611
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface AfterAssertionErrorCollected {
* assertion errors collection is implemented.
* <p>
* If you just use the standard soft assertions classes provided by AssertJ, you can register your callback with
* {@link AbstractSoftAssertions#setAfterAssertionErrorCollected(AfterAssertionErrorCollected)}.
* {@link AbstractSoftAssertions#addAfterAssertionErrorCollected(AfterAssertionErrorCollected)}.
* <p>
* Example with custom soft assertions:
* <pre><code class='java'> class TolkienSoftAssertions extends SoftAssertions {
Expand All @@ -51,7 +51,7 @@ public interface AfterAssertionErrorCollected {
* <pre><code class='java'> SoftAssertions softly = new SoftAssertions();
*
* // register our callback
* softly.setAfterAssertionErrorCollected(error -&gt; System.out.println(error));
* softly.addAfterAssertionErrorCollected(error -&gt; System.out.println(error));
*
* // the AssertionError corresponding to this failing assertion is printed to the console.
* softly.assertThat("The Beatles").isEqualTo("The Rolling Stones");</code></pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import static java.util.Collections.unmodifiableList;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.assertj.core.util.Throwables;

Expand All @@ -30,7 +28,7 @@ public class DefaultAssertionErrorCollector implements AssertionErrorCollector {
private volatile boolean wasSuccess = true;
private List<AssertionError> collectedAssertionErrors = synchronizedList(new ArrayList<>());

private Set<AfterAssertionErrorCollected> callbacks = new HashSet<>();
private List<AfterAssertionErrorCollected> callbacks = synchronizedList(new ArrayList<>());

private AssertionErrorCollector delegate = null;

Expand Down Expand Up @@ -77,6 +75,22 @@ public List<AssertionError> assertionErrorsCollected() {
return decorateErrorsCollected(errors);
}

/**
* Same as {@link DefaultAssertionErrorCollector#addAfterAssertionErrorCollected(AfterAssertionErrorCollected)}, but
* also removes all previously added callbacks. Please consider using
* {@link DefaultAssertionErrorCollector#addAfterAssertionErrorCollected(AfterAssertionErrorCollected)}
* instead, because another frameworks and integrations can also use this functionality.
*
* @param afterAssertionErrorCollected the callback.
*
* @since 3.17.0
* @deprecated
*/
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) {
callbacks.clear();
addAfterAssertionErrorCollected(afterAssertionErrorCollected);
}

/**
* Register a callback allowing to react after an {@link AssertionError} is collected by the current soft assertion.
* <p>
Expand All @@ -85,7 +99,7 @@ public List<AssertionError> assertionErrorsCollected() {
* Example:
* <pre><code class='java'> SoftAssertions softly = new SoftAssertions();
* StringBuilder reportBuilder = new StringBuilder(format("Assertions report:%n"));
* // register our callback
* softly.setAfterAssertionErrorCollected(error -&gt; reportBuilder.append(String.format("------------------%n%s%n", error.getMessage())));
*
Expand Down Expand Up @@ -114,9 +128,9 @@ public List<AssertionError> assertionErrorsCollected() {
*
* @param afterAssertionErrorCollected the callback.
*
* @since 3.17.0
* @since 3.26.0
*/
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) {
public void addAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected) {
callbacks.add(afterAssertionErrorCollected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,54 @@

import static org.assertj.core.api.BDDAssertions.then;

class DefaultAssertionErrorCollector_setAfterAssertionErrorCollected_Test {
private List<String> stringList;
private List<Throwable> throwableList;
class DefaultAssertionErrorCollector_addAfterAssertionErrorCollected_Test {
private List<String> errorMessages;
private List<Throwable> throwables;
private SoftAssertions softly;

@BeforeEach
void given() {
stringList = new ArrayList<>();
throwableList = new ArrayList<>();
errorMessages = new ArrayList<>();
throwables = new ArrayList<>();
softly = new SoftAssertions();
}

@Test
void should_perform_both_specified_actions_on_each_assertion_error() {
// WHEN
softly.setAfterAssertionErrorCollected(err -> stringList.add(err.getMessage()));
softly.setAfterAssertionErrorCollected(throwableList::add);
softly.addAfterAssertionErrorCollected(err -> errorMessages.add(err.getMessage()));
softly.addAfterAssertionErrorCollected(throwables::add);
softly.assertThat(1)
.isEqualTo(1_000)
.isBetween(7, 15)
.isEven();
// THEN
then(stringList).hasSameSizeAs(throwableList).hasSize(3);
then(errorMessages).hasSameSizeAs(throwables)
.hasSize(3);
}

@Test
void should_execute_both_specified_actions_on_each_manually_added_error() {
// WHEN
softly.setAfterAssertionErrorCollected(err -> stringList.add(err.getMessage()));
softly.setAfterAssertionErrorCollected(throwableList::add);
softly.addAfterAssertionErrorCollected(err -> errorMessages.add(err.getMessage()));
softly.addAfterAssertionErrorCollected(throwables::add);
softly.collectAssertionError(new AssertionError("hello"));
softly.collectAssertionError(new AssertionError("world"));
// THEN
then(stringList).hasSameSizeAs(throwableList).hasSize(2);
then(errorMessages).hasSameSizeAs(throwables)
.hasSize(2);
}

@Test
void should_pass_if_the_callback_object_can_be_set_only_one_time() {
void should_register_the_same_callback_several_times() {
// GIVEN
AfterAssertionErrorCollected callback = throwableList::add;
AfterAssertionErrorCollected callback = throwables::add;
// WHEN
for (int i = 0; i < 20; i++) {
softly.setAfterAssertionErrorCollected(callback);
for (int i = 0; i < 10; i++) {
softly.addAfterAssertionErrorCollected(callback);
}
softly.collectAssertionError(new AssertionError("hello"));
// THEN
then(throwableList).hasSize(1);
then(throwables).hasSize(10);
}
}

0 comments on commit a5f9611

Please sign in to comment.