Search code examples
assertj

AssertJ Verify List of Objects with a List field is not empty


Example:

public class Office {
  List<Employee> employee;
}

How do I assert that in my List<Office> offices there are none with no employees? Is it possible to assert this with one assertion chain?


Solution

  • If I understand your question correctly you want to check that all offices have employee, if so allSatisfy can be used like:

    assertThat(offices).allSatisfy(office -> {
                                  assertThat(office.employee).isNotEmpty();
                                });
    

    There is also a noneSatisfy assertion available BTW.