I didn't find any example of using satisfiesExactlyInAnyOrder assertion of assertj, so here's a simple example:
I wanted to check that the actual list contains strings that end with the strings of the expected list, in any order.
Example of a valid test: expected = {"abc","def","ghi"}, actual = {"1=def","ghi","234=abc"}
package test;
import org.assertj.core.api.Assertions;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class Test1 {
private static void testSuffixes(List<String> expected, List<String> actual) {
// Turn the expected strings into consumers
Consumer<String>[] suffixTests = expected
.stream()
.map(expected -> {
Consumer<String> suffixTest =
value -> Assertions.assertThat(value).endsWith(expected);
return suffixTest;
})
.collect(Collectors.toList())
.toArray(new Consumer[0]);
Assertions.assertThat(actual).satisfiesExactlyInAnyOrder(suffixTests);
}
public static void main(String[] args) {
List<String> expected = List.of("ABC", "DEF", "GHI");
List<String> actual = List.of("0=ABC", "3=GHI", "DEF");
testSuffixes(expected, actual);
}
}
When the actual values end with the expected strings, the execution ends with code 0. When the actual values don't end with the expected strings, the execution ends with AssertionError:
Exception in thread "main" java.lang.AssertionError: Expecting actual: ["0=ABC", "DEF1", "3=GHI"] to satisfy all the consumers in any order.