我有Grails控制器单元测试,什么都不做。 控制器有一个用@Inject注释的字段。 由于依赖注入失败,测试崩溃了。 但据我所知,DI不应该用于这种测试。 依赖关系是在测试方法的 given:部分手动设置的。
那我该怎么办?如何在单元测试中关闭DI?
这是我的测试:
@TestFor(MstarSearchController)
class MstarSearchControllerSpec extends Specification {
void "test something"() {
expect:
3 == 3
}
}
我的控制员:
class MstarSearchController {
@Autowired(required = true)
MstarSearchService mstarSearch
命令 grails test-app 因未满足的依赖项而崩溃。
test something(com.aspect.dscadmin.MstarSearchControllerSpec)
|
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aspect.dscadmin.MstarSearchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.aspect.dscadmin.MstarSearchService com.aspect.dscadmin.MstarSearchController.mstarSearch; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aspect.dscadmin.MstarSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at grails.test.mixin.web.ControllerUnitTestMixin.mockController_closure2(ControllerUnitTestMixin.groovy:185)
at grails.test.mixin.web.ControllerUnitTestMixin.mockController(ControllerUnitTestMixin.groovy:193)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.MethodInvocation.invokeTargetMethod(MethodInvocation.java:91)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:85)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$1$2.evaluate(TestRuntimeJunitAdapter.groovy:49)
at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.MethodInvocation.invokeTargetMethod(MethodInvocation.java:91)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:85)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$3$4.evaluate(TestRuntimeJunitAdapter.groovy:76)
at org.spockframework.runtime.extension.builtin.ClassRuleInterceptor.intercept(ClassRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.aspect.dscadmin.MstarSearchService com.aspect.dscadmin.MstarSearchController.mstarSearch; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aspect.dscadmin.MstarSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
... 20 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aspect.dscadmin.MstarSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
... 20 more
答案 0 :(得分:2)
我有这个问题。似乎通过约定,基于测试的名称,Spock自动实例化一个测试对象并试图在我做任何事情之前注入所有依赖项以防止它。如果您将测试的名称更改为与测试类不对应并删除@TestFor注释,则可以实例化测试对象,实例化MstarSearch对象,并在设置中手动分配测试对象的mstarSearch没有触发@Autowired。