Skip to content

Instantly share code, notes, and snippets.

@eddarmitage
Last active December 3, 2016 22:44
Show Gist options
  • Save eddarmitage/98f1bd0496768243230a859b0e46969b to your computer and use it in GitHub Desktop.
Save eddarmitage/98f1bd0496768243230a859b0e46969b to your computer and use it in GitHub Desktop.
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
public class AllSoftlyMatchTest {
@Test
public void shouldFailTwice() {
String story = "Bob, Jim and Carol went to the shops";
List<String> characters = Arrays.asList("Jim", "John", "Jeremy");
assertThat(characters).allSoftlyMatch(story::contains, "character is in story");
// Output of allMatch():
/*
* Expecting all elements of:
* <["Jim", "John", "Jeremy"]>
* to match 'character is in story' predicate but this element did not:
* <"John">
*/
}
@Test
public void currentAlternative() {
String story = "Bob, Jim and Carol went to the shops";
List<String> characters = Arrays.asList("Jim", "John", "Jeremy");
assertSoftly(softly -> {
characters.forEach(character -> softly.assertThat(story).contains(character));
});
// Output:
/*
* The following 2 assertions failed:
* 1)
* Expecting:
* <"Bob, Jim and Carol went to the shops">
* to contain:
* <"John">
* at JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
* 2)
* Expecting:
* <"Bob, Jim and Carol went to the shops">
* to contain:
* <"Jeremy">
* at JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
*/
}
@joel-costigliola
Copy link

joel-costigliola commented Dec 3, 2016

This should be more an improvement of allMatch than a new assertions, said otherwise allMatch should report all the elements not matching the predicate. assertj/assertj#816

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment