Thursday, July 15, 2010

Grails 1.3.3 - testing domain classes FIX

Hi there,

if you're following the TDD principles while using Grails you might be very surprised when upgrading to Grails 1.3.3 because all your unit tests that mocked domain classes and utilized the "save()" instance method all of the sudden fail with some crazy message:

at grails.test.MockUtils.mockDomain(MockUtils.groovy:443)
at grails.test.MockUtils$ Source)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:131)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:129)

It's obviously a bug, but don't fear - help is coming!

The bug description in Jira GRAILS-6482 has the answer!

I've allowed myself to go one step further and I've created a base class for those cases where it's needed (DRY - remember?)

package grails.test

import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.codehaus.groovy.grails.plugins.PluginManagerHolder

class FixedGrailsUnitTestCase extends GrailsUnitTestCase {

protected void setUp() {
PluginManagerHolder.pluginManager = [
hasGrailsPlugin: { String name -> true }
] as GrailsPluginManager

protected void tearDown() {
PluginManagerHolder.pluginManager = null

Now all my test classes that were touched by this issue inherit from FixedGrailsUnitTestCase and life is good again :)

I hope this will spare you a few minutes :)

No comments: