diff --git a/src/main/java/org/assertj/core/error/NoElementsShouldSatisfy.java b/src/main/java/org/assertj/core/error/NoElementsShouldSatisfy.java index 925973ed932..bbaa5436a3d 100644 --- a/src/main/java/org/assertj/core/error/NoElementsShouldSatisfy.java +++ b/src/main/java/org/assertj/core/error/NoElementsShouldSatisfy.java @@ -22,7 +22,7 @@ private NoElementsShouldSatisfy(Object actual, Object faultyElement) { super("%n" + "Expecting no elements of:%n" + " <%s>%n" + - "to satisfy the given assertions requirements but this one did:%n" + + "to satisfy the given assertions requirements but these elements did:%n" + " <%s>", actual, faultyElement); } diff --git a/src/main/java/org/assertj/core/internal/Iterables.java b/src/main/java/org/assertj/core/internal/Iterables.java index 136dc996ef0..92ff2ee3dbd 100644 --- a/src/main/java/org/assertj/core/internal/Iterables.java +++ b/src/main/java/org/assertj/core/internal/Iterables.java @@ -1188,19 +1188,22 @@ public void assertAllMatch(AssertionInfo info, Iterable actual, public void assertNoneSatisfy(AssertionInfo info, Iterable actual, Consumer restrictions) { assertNotNull(info, actual); requireNonNull(restrictions, "The Consumer expressing the restrictions must not be null"); - actual.forEach(element -> failIfElementSatisfyRestrictions(element, restrictions, actual, info)); + List erroneousElements = stream(actual).map(element -> failsRestrictions(element, restrictions)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(toList()); + if (erroneousElements.size() > 0) throw failures.failure(info, noElementsShouldSatisfy(actual, erroneousElements)); } - private void failIfElementSatisfyRestrictions(E element, Consumer restrictions, Iterable actual, - AssertionInfo info) { + private Optional failsRestrictions(E element, Consumer restrictions) { try { restrictions.accept(element); } catch (AssertionError e) { - // no problem, element is supposed not to meet the given restrictions - return; + // element is supposed not to meet the given restrictions + return Optional.empty(); } - // problem: element meets the given restrictions! - throw failures.failure(info, noElementsShouldSatisfy(actual, element)); + // element meets the given restrictions! + return Optional.of(element); } public void assertAnyMatch(AssertionInfo info, Iterable actual, Predicate predicate, diff --git a/src/test/java/org/assertj/core/error/NoElementsShouldSatisfy_create_Test.java b/src/test/java/org/assertj/core/error/NoElementsShouldSatisfy_create_Test.java index 8de5f67e88c..1fe4d474756 100644 --- a/src/test/java/org/assertj/core/error/NoElementsShouldSatisfy_create_Test.java +++ b/src/test/java/org/assertj/core/error/NoElementsShouldSatisfy_create_Test.java @@ -16,7 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.error.NoElementsShouldSatisfy.noElementsShouldSatisfy; import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; -import static org.assertj.core.util.Lists.newArrayList; +import static org.assertj.core.util.Lists.list; import org.assertj.core.description.TextDescription; import org.junit.jupiter.api.Test; @@ -24,16 +24,30 @@ public class NoElementsShouldSatisfy_create_Test { @Test - public void should_create_error_message_any() { + public void should_create_error_message() { // GIVEN - ErrorMessageFactory factory = noElementsShouldSatisfy(newArrayList("Luke", "Yoda"), "Vader"); + ErrorMessageFactory factory = noElementsShouldSatisfy(list("Luke", "Leia", "Yoda"), list("Luke", "Leia")); // WHEN String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); // THEN assertThat(message).isEqualTo(format("[Test] %n" + "Expecting no elements of:%n" + - " <[\"Luke\", \"Yoda\"]>%n" + - "to satisfy the given assertions requirements but this one did:%n" + - " <\"Vader\">")); + " <[\"Luke\", \"Leia\", \"Yoda\"]>%n" + + "to satisfy the given assertions requirements but these elements did:%n" + + " <[\"Luke\", \"Leia\"]>")); + } + + @Test + public void should_create_error_message_percent() { + // GIVEN + ErrorMessageFactory factory = noElementsShouldSatisfy(list("Luke", "Leia%s", "Yoda"), list("Luke", "Leia%s")); + // WHEN + String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); + // THEN + assertThat(message).isEqualTo(format("[Test] %n" + + "Expecting no elements of:%n" + + " <[\"Luke\", \"Leia%%s\", \"Yoda\"]>%n" + + "to satisfy the given assertions requirements but these elements did:%n" + + " <[\"Luke\", \"Leia%%s\"]>")); } } diff --git a/src/test/java/org/assertj/core/internal/iterables/Iterables_assertNoneSatisfy_Test.java b/src/test/java/org/assertj/core/internal/iterables/Iterables_assertNoneSatisfy_Test.java index 0ad8c003b4a..cde4fc0d6d8 100644 --- a/src/test/java/org/assertj/core/internal/iterables/Iterables_assertNoneSatisfy_Test.java +++ b/src/test/java/org/assertj/core/internal/iterables/Iterables_assertNoneSatisfy_Test.java @@ -15,11 +15,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.error.NoElementsShouldSatisfy.noElementsShouldSatisfy; import static org.assertj.core.test.TestData.someInfo; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.newArrayList; +import static org.assertj.core.util.Lists.list; import static org.mockito.Mockito.verify; import java.util.List; @@ -30,7 +30,7 @@ public class Iterables_assertNoneSatisfy_Test extends IterablesBaseTest { - private List actual = newArrayList("Luke", "Leia", "Yoda"); + private List actual = list("Luke", "Leia", "Yoda"); @Test public void should_pass_when_no_elements_satisfy_the_given_single_restriction() { @@ -65,12 +65,22 @@ public void should_fail_when_one_elements_satisfy_the_given_restrictions() { // GIVEN Consumer restrictions = name -> assertThat(name).startsWith("Y"); // WHEN - Throwable assertionError = catchThrowable(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions)); + Throwable assertionError = expectAssertionError(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions)); // THEN - verify(failures).failure(info, noElementsShouldSatisfy(actual, "Yoda")); + verify(failures).failure(info, noElementsShouldSatisfy(actual, list("Yoda"))); assertThat(assertionError).isNotNull(); } + @Test + public void should_fail_when_two_elements_satisfy_the_given_restrictions() { + // GIVEN + Consumer restrictions = name -> assertThat(name).startsWith("L"); + // WHEN + expectAssertionError(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions)); + // THEN + verify(failures).failure(info, noElementsShouldSatisfy(actual, list("Luke", "Leia"))); + } + @Test public void should_throw_error_if_consumer_restrictions_is_null() { assertThatNullPointerException().isThrownBy(() -> iterables.assertNoneSatisfy(someInfo(), actual, null)) @@ -79,7 +89,7 @@ public void should_throw_error_if_consumer_restrictions_is_null() { @Test public void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->{ + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { List nullActual = null; iterables.assertNoneSatisfy(someInfo(), nullActual, name -> assertThat(name).startsWith("Y")); }).withMessage(actualIsNull());