I have a grails unit test that has code similar to below and is appears that setting fields via getter/setter methods doesn't work with constructors (even though it actually works with non-domain classes).
I understand that the following works with properties:
class Person {
def firstName
def lastName
def getFullName() {
return "$firstName $lastName"
}
def setFullName(name) {
firstName = name.split(" ")[0]
lastName = name.split(" ")[1]
}
}
def = new Person(fisrtName: "Joe", lastName: "Bloggs")
But when I do the following the first and last name fields don't get set:
def = new Person(fullName: "Joe Bloggs")
Is there a way to set fields via methods in a groovy contstructor?
Best Answer
What version of groovy are you using? This works fine for me with groovy 1.8.6 and I think it's worked for that way for a very long time:
Updated:
Just tried this on grails 2.0.3. You need to be more explicit in your method signatures for grails to work. I changed the method signature for the getter to be
String
and the setter to bevoid
and it worked. It did not work with justdef
. Grails 2 is much more strict about matching signatures than previous versions of grails were and I'm betting that this is part of it.Also, you should specify that the
fullName
"property" is transient as it isn't a real property that should get persisted in the database. Here's the domain and test class that work for me in grails 2.0.3:Person.groovy:
PersonTests.groovy: