Cake { @Override public String toString() { return String.format("id=%s topping=%s layers=%s", id, topping, layers.toString()); } Cake(); Long getId(); void setId(Long id); CakeTopping getTopping(); void setTopping(CakeTopping topping); Set<CakeLayer> getLayers(); final void setLayers(Set<CakeLayer> layers); void addLayer(CakeLayer layer); @Override String toString();  }
@Test public void testToString() { final CakeTopping topping = new CakeTopping("topping", 20); topping.setId(2345L); final CakeLayer layer = new CakeLayer("layer", 100); layer.setId(3456L); final Cake cake = new Cake(); cake.setId(1234L); cake.setTopping(topping); cake.addLayer(layer); final String expected = "id=1234 topping=id=2345 name=topping calories=20 " + "layers=[id=3456 name=layer calories=100]"; assertEquals(expected, cake.toString()); }