Вы можете решить это с помощью allSatisfy
повторяемого утверждения , как показано в следующем примере:
@Test
public void assertInnerPropertyInList() {
List<Office> officesWithEmptyOne = List.of(
new Office(List.of(new Employee(), new Employee())),
new Office(List.of(new Employee())),
new Office(List.of()));
List<Office> offices = List.of(
new Office(List.of(new Employee(), new Employee())),
new Office(List.of(new Employee())));
// passes
assertThat(offices).allSatisfy(office -> assertThat(office.getEmployee()).isNotEmpty());
// fails
assertThat(officesWithEmptyOne).allSatisfy(office -> assertThat(office.getEmployee()).isNotEmpty());
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Office {
private List<Employee> employee;
}
@Data
@AllArgsConstructor
public class Employee {
}
И вы можете увидеть, что второе утверждение завершается неудачно с сообщением:
java.lang.AssertionError:
Expecting all elements of:
<[AssertJFeatureTest.Office(employee=[AssertJFeatureTest.Employee(), AssertJFeatureTest.Employee()]),
AssertJFeatureTest.Office(employee=[AssertJFeatureTest.Employee()]),
AssertJFeatureTest.Office(employee=[])]>
to satisfy given requirements, but these elements did not:
<AssertJFeatureTest.Office(employee=[])>
Expecting actual not to be empty
Аннотации приходят с Ломбока.