tags:

views:

539

answers:

2
+3  A: 

In your test parent.children will always be null (children wont be initialized until you add the first one). So you could change your test to:

assertNull parent.children

The children will be initialized when you save the Parent (regardless if children are added) or when children are added. If you want it to always be initialized you can do it manually in the Parent when you define the children:

List<Children> children = new ArrayList<Children>()
John Wagenleitner
But how do I initialize the empty List properly? There is a difference between null and an empty List. There should be a way to get parent.children to return an object and not null without having to add a dummy child.
I understand there is a difference between empty and null, Grails will initialize once you save the parent (regardless if a child is added) or when you add a child. If it's important to you that the list is always initialized you can initialize it yourself in the Parent class. See my edited answer.
John Wagenleitner
A: 

i am getting similar results (maybe children stays null). note that you must be using an integration test for the gorm stuff to be around.

you can handle an empty list as follows:

parent.children?.each { println it}
parent.addToChildren(new Children(/* whatever you need */))
parent.children?.each { println it}
Ray Tayek