views:

115

answers:

1

Hi,

In a Grails 1.1 plugin, I'm trying to load a class from the main application using the following code:

class MyClass {
  static Map getCustomConfig(String configName){
    return new ConfigSlurper().
      parse(ApplicationHolder.application.classLoader.loadClass(configName))           
  }
}

Where configName is the name of the class in $MAIN_APP/grails-app/conf containing the configuration info. However, when the code above runs within a unit test applicationHolder.application returns null, causing the method above to throw a NullPointerException. A Grails JIRA issue was created for this problem, but it has been marked as fixed despite the fact that problem appears to still exist.

I know that within the plugin descriptor class I can access the main application (an instance of GrailsApplication) via the implicit application variable. But the code shown above is in not in the plugin descriptor.

Is there a way that I can load a class from the main application within a plugin (but outside the plugin descriptor)?

Thanks, Don

A: 

It turns out there are 2 possible answers.

The Right Answer

GrailsApplication is not available in unit tests, so for the code above to work it should be an integration test

The Hack that Works

Change

parse(ApplicationHolder.application.classLoader.loadClass(configName))

to

parse(MyClass.classLoader.loadClass(configName))
Don