ObjectGraphBuilder is a builder for an arbitrary graph of beans that follow the JavaBean convention, its useful for creating test data for example.
Let's say that the following classes belong into your domain model:
With ObjectGraphBuilder building a Company with three employees is as easy as
Here is what's happening behind the scenes:
All 4 strategies have a default implementation that work as expected if the code follows the usual conventions for writing JavaBeans. But if by any chance any of your beans does not follow the convention you may plug your own implementation of each strategy. Each strategy setter is Closure friendly, for example
ObjectGraphBuilder supports ids per node as SwingBuilder does, meaning that you can 'store' a reference to a node in the builder, this is useful to relate one instance with many others as well. Because a property named 'id' may be of business meaning in some domain models ObjectGraphBuilder has a strategy named IdentifierResolver that you may configure to change the default name value ('id'). The same may happen with the property used for referencing a previously saved instance, a strategy named ReferenceResolver will yield the appropriate value (default is 'refId'):
Its worth mentioning that you cannot modify the properties of a referenced bean.
For those rare occasions where ObjectGraphBuilder can't locate your classes (it happens when you run a script using groovyConsole) you may define a classLoader for ObjectGraphBuilder to resolve classes. Try for example running the following script inside groovyConsole and then comment out the classLoader property.