Reputation: 157
I am looking for a convenient way of matching list of objects to list of conditions or assertions in a way that all records could be in any order, but all of them should match to all specified conditions. Specific object to specific condition.
Example:
I have list of objects, that could be fetched in arbitrary order. Let's say of the following type:
class Person {
String name;
Integer age;
}
Person A is Bobby, 34y
Person B is John, 15y
And I have list of conditions that I expect to match.
Condition personA = new Condition<>(p -> p.name.equals("Bobby"), "Person A");
Condition personB = new Condition<>(p -> p.name.equals("john"), "Person B");
And now I would like to have some operator that will take a list of persons, and match each person to list of conditions. One of condition should pass, otherwise - assertion fail. Order of persons and conditions could be arbitrary.
Is there exists any way how to do this without adding custom implementation?
P.S> I am aware of extracting() method, but think it is not that convenient for comparing complex objects.
Upvotes: 1
Views: 1662
Reputation: 6463
You can test that at least one of the conditions personA
and personB
is met for each one of the elements in the List <Person>
personList
as follows:
assertThat( personList ).are( anyOf( personA, personB ) );
See the AssertJ documentation.
Note the following imports are required:
import static org.assertj.core.api.Assertions.anyOf;
import static org.assertj.core.api.Assertions.assertThat;
Upvotes: 4