Mindblow: Assert nested objects with AssertJ
November 22, 2020Nick

Mindblow: Assert nested objects with AssertJ

Use one assertion to check a deep nested object for specific values

AssertJ is very nice for test-driven-development or commonly testing in Java and today I figured out a mindblowing feature. And to be honest, if I had read the documentation, I would have known it already 😎

class Item {
  // ...
}

class Table {
  int legs;
  String color;
  List<Item> servedItems;

  // ... Ctor, Getter, Setter
}

Table table = new Table(4, "brown", Arrays.asList(new Item(/* ... */), new Item(/* ... */)));

assertThat(table)
    .extracting("color", "servedItems.size")
    .containsExactly("brown", 2);

In the example you can see: It's possible to access the item list in table as String in dot notation. 🤯 Nice job girls and boys from AssertJ

Nick

Written by Nick

Professional Code Breaker & AssertJ squeezer

More ways to Suck at Coding