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:

java.lang.NullPointerException
at grails.test.MockUtils.mockDomain(MockUtils.groovy:443)
at grails.test.MockUtils$mockDomain.call(Unknown 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() {
super.setUp();
PluginManagerHolder.pluginManager = [
hasGrailsPlugin: { String name -> true }
] as GrailsPluginManager
}

protected void tearDown() {
super.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 :)

Sunday, July 11, 2010

Grails and content negotiation

Hi all,

I've been going through the user's manual for Grails and was shocked that this little piece has slipped my study before. Content negotiation is done in Grails in the most simple and user-friendly way and above all it's almost identical to Ruby on Rails!

I've recently blogged on how to achieve a similar result using ASP.NET MVC (which does require certain amount of manual work)

In Grails everything you need is already built-in. The engine recognizes extensions passed on in the URL automatically as well as request headers (if properly specified by client) and the "withFormat" construct resembles 100% its counterpart in RoR.

The longer I use and study Grails the more I see the power behind it. Even though it might be unstable from version to version (especially the major ones) it's still one of the most comprehensive, well designed and user-friendly web application platforms ever created. I love it!

Have fun!