views:

34

answers:

1

I'm having hard time mocking the save instance method in my unit tests in Grails 1.3.3. I've created a simple domain class named Person, it has one property (nullable) called "name".

package tutorial

class Person {

    String name

    static constraints = {
        name nullable: true
    }
}

In my test I'm trying to do something I've found in the documentation:

class PersonTests extends GrailsUnitTestCase {
    public void testCanSavePerson() {
        def testInstances = []
        mockDomain(Person, testInstances)
        assertEquals(0, Person.count())
        new Person(name: "Bob").save()
        assertEquals(1, Person.count())
    }
}

However as soon as I run the test what I get is an exception:

java.lang.NullPointerException at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973) at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014) at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy:736) at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy) at tutorial.PersonTests.testCanSavePerson(PersonTests.groovy:25)

whereas the line 25 is exactly the one that calls save() on newly created instance.

Does anyone knows what am I doing wrong?

A: 

This is an already known bug in Grails 1.3.3. Read more about it and find a workaround in the associated JIRA ticket GRAILS-6482.

codescape
Thanks! The workaround worked for me.
Matthias Hryniszak