Grails集成测试中的依赖注入 [英] Dependency injection in Grails integration tests
问题描述
我正在测试应用程序的服务,这些服务在运行时依赖于其他服务。测试时,依赖注入似乎不起作用。在运行集成测试时,依赖注入在Grails构件中起作用吗?解决方案
是的, ,应用程序被启动并且所有的bean都被创建并注入,就像应用程序实际运行一样。
当然,如果你实例化一个需要使用'你的测试你不会得到DI的好处。相反,在你的测试用例中为你的测试bean创建一个属性,它将被注入:
class MyServiceTests extends GrailsUnitTestCase {
MyService服务
void testInjection(){
assertNotNull服务
}
}
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?
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
}
}
这篇关于Grails集成测试中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!