AssertJ: For a Pojo how to check each nested Property/Field in one chained sentence

I think the best way is to extract all the properties/fields and then checks it does not contain null.

Example:

TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);

// support nested properties: 
assertThat(frodo).extracting("name", "age", "race.name")
                 .doesNotContainNull()
                 .containsExactly("Frodo", 33, "Hobbit");

Note that you can also use lambdas to extract values from the object under test.

assertThat(frodo).extracting(TolkienCharacter::getName,
                             character -> character.age,
                             character -> character.getRace().getName())
                 .containsExactly("Frodo", 33, "Hobbit");

Leave a Comment

tech