gpt4 book ai didi

unit-testing - 为什么 Controller 单元测试会启动 DI?

转载 作者:行者123 更新时间:2023-12-02 13:47:43 25 4
gpt4 key购买 nike

我有一个 Grails Controller 单元测试,它什么都不做。 Controller 有一个用@Inject 注释的字段。由于依赖项注入(inject)失败,测试崩溃。但据我了解,DI 不应该用于此类测试。在测试方法的 given: 部分手动设置依赖项。

那我做错了什么?如何在单元测试中关闭 DI?

这是我的测试:

@TestFor(MstarSearchController)
class MstarSearchControllerSpec extends Specification {
void "test something"() {
expect:
3 == 3
}
}

还有我的 Controller :

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

最佳答案

我遇到了这个问题。似乎,按照惯例,Spock 会根据测试的名称自动实例化一个测试对象,并在我采取任何措施阻止它之前尝试注入(inject)所有依赖项。如果您将测试的名称更改为与测试类不对应并删除@TestFor 注释,您可以实例化测试对象,实例化一个 MstarSearch 对象,并在您的设置中手动分配测试对象的 mstarSearch,而无需触发 @Autowired。

关于unit-testing - 为什么 Controller 单元测试会启动 DI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com