other register

Thursday, October 20, 2011

Cannot get property 'config' on null object in Grails Integration Test

Class someService {

def grailsApplication

def someMethod() {

def attribute = grailsApplication.config.someAttribute
return attribute

When running integration test,

Class someServiceTests {

void testSomeMethod() {
def service = new SomeService()
def attribute = service.someMethod()

It will complain that:

"java.lang.NullPointerException: Cannot get property 'config' on null object"

The way around this is, in the integration test, add the following line:

service.grailsApplication = new DefaultGrailsApplication()

No comments: