Unit testing with JAXB

I hit a snag when unit testing some code that makes use of JAXB generated objects. JAXB does not generate setters for collections 😛 This means that you cannot do the following:

Parent parent = new Parent();
List<Child> children = new ArrayList<Child>();
children.add(new Child());
parent.setChildren(children); // there's no setter method!

Instead, the following will do the trick, since JAXB guarantees that getChildren() will always return a Collection:

Parent parent = new Parent();
List<Child> children = new ArrayList<Child>();
children.add(new Child());
parent.getChildren().addAll(children);

Hope this saves someone some frustration.


Posted

in

by

Tags: