I have a service that needs the scope "session". So I simply use
class MyService {
static scope = "session"
...
}
But in my integration test it doesn't get wired correctly:
class MyServiceIntegrationSpec extends IntegrationSpec {
def myService
...
}
I always get the error:
Error creating bean with name 'myService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;
What am I doing wrong?
You cannot inject 'lower' scoped bean into singleton. It's like you would like to inject controller into service. Controller is created with each request, but service is one for the application (if singleton by default). Here you can have different sessions, but only one singleton for test - so service from which session would you like to use in test?
It can be done getting the bean from the context instead of injecting it:
def grailsApplication
void testSomething() {
given:
def myService = grailsApplication.mainContext.getBean('myService')
...
Changing the scope of the test from singleton to session would also resolve the problem, but I don't know if this is possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With