register
other register

Saturday, October 22, 2011

Grails Service Singleton

Jeff Brown explained Grails singleton service in Nabble. Below is his description.

"If your service has an instance variable (that is state) and you don't want that state to be shared by multiple concurrent users of the service, then you don't want that service to be a singleton. If a service is a singleton then every user of that service is sharing the same copy of the service. If a service is request scoped then a new instance is created for every http request. If a service is sessions scoped then the service will be kept around and reused for the entire user session (each user session has their own copy). "

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 {

@Test
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()