Sunday, December 19, 2010

Expando to the rescue

As we incorporate more plugins in our environment, one of the issues we faced was on how to test code executed by plugins. Most well written plugins can be auto injected into your controllers and the implementing class is generally not exposed. So without knowing the implementing class, mocking it out would not be possible.

We came across something called Expando and the more we use it, the more I'm falling in love with it. Lets give a concrete example using the Background Service plugin

Say we have a service using the background service plugin.

class SomeService {

def backgroundService


def doSomething(){
backgroundService.execute("Asynch process", {

Now to test this, add expando to your test class and execute the closure

def expando = new Expando()
expando.execute={ info, clos ->
assertEquals "log message match expected", "Asynch process", "${info}"
clos() //This will execute the closure so in effect calling doAsynch

someService.backgroundService = expando

So if you are ever considering choosing Groovy on Grails vs Java, Expando is grails' x-factor to tilt the decision.

No comments: