Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>class Person { def firstName def lastName def getFullName() { return "$firstName $lastName" } def setFullName(name) { firstName = name.split(" ")[0] lastName = name.split(" ")[1] } } def p1 = new Person(firstName: "Joe", lastName: "Bloggs") def p2 = new Person(fullName: "Joe Bloggs") assert p1.firstName == p2.firstName assert p1.lastName == p2.lastName </code></pre> <p>Updated:</p> <p>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 <code>String</code> and the setter to be <code>void</code> and it worked. It did not work with just <code>def</code>. 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.</p> <p>Also, you should specify that the <code>fullName</code> "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:</p> <p>Person.groovy:</p> <pre><code>package com.example class Person { String firstName String lastName static transients = ["fullName"] String getFullName() { return "$firstName $lastName" } void setFullName(String name) { firstName = name.split(" ")[0] lastName = name.split(" ")[1] } } </code></pre> <p>PersonTests.groovy:</p> <pre><code>package com.example import grails.test.mixin.* import org.junit.* /** * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions */ @TestFor(Person) @Mock([Person]) class PersonTests { void testFullName() { Person p1 = new Person(firstName: "Joe", lastName: "Bloggs").save(failOnError: true) Person p2 = new Person(fullName: "Joe Bloggs").save(failOnError: true) assert p1.firstName == p2.firstName assert p1.lastName == p2.lastName assert p1.fullName == p2.fullName } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload