views:

286

answers:

1

I'm testing a service of my application thats depends of another services in runtime. When testing, the dependency inject seems doesn't works. Does dependency injection works in Grails artefacts when running integration tests?

+4  A: 

Yes, when running tests (ie those in the integration directory), the application is started and all beans are created and injected as if the app were actually running. The only difference between the test app and the running app should be the configuration environment.

Of course, if you instantiate a class that requires injection using the 'new' operator in your test you won't get the benefits of DI. Instead, create a property in your test case for the bean your testing and it will be injected:

class MyServiceTests extends GrailsUnitTestCase {

    MyService service

    void testInjection() {
        assertNotNull service
    }
}
Dave
Thanks. I'm testing a service that depends of anothers services and i was instantiating it and because of this, i wasn't get the benefits of DI.
Lucas