Load application class from plugin

In the Grails 1.1 plugin, I am 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

which contains configuration information. However, when the above code runs in a unit test applicationHolder.application

, it returns null, causing the above method to reset NullPointerException

. A Grails A JIRA issue was created for this issue, but it has been marked fixed even though the issue still exists.

I know that in the plugin descriptor class, I can access the main application (instance GrailsApplication

) via an implicit variable application

. But the code shown above is not in the plugin descriptor.

Is there a way to load the class from the main application into the plugin (but outside the plugin descriptor)?

Thanks Don

0


a source to share


1 answer


It turns out there are 2 possible answers.

Correct answer

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

A hack that works



Edit

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

      

to

parse(MyClass.classLoader.loadClass(configName))

      

0


a source







All Articles