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
2010-02-16 14:30:53
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
2010-02-16 15:37:27