grails服务单元测试自动布线
我有一个服务正在扩展基本服务,而在基本服务中,又有一个服务连接了@Autowired注释,因为grails服务单元测试没有通过grails服务单元测试自动布线,grails,Grails,我有一个服务正在扩展基本服务,而在基本服务中,又有一个服务连接了@Autowired注释,因为grails服务单元测试没有通过 class UserService extends BaseService{ def getUser() { def u = [:] u.name='jason' return u } } import org.springframework.beans.factory.annotation.Autowired clas
class UserService extends BaseService{
def getUser() {
def u = [:]
u.name='jason'
return u
}
}
import org.springframework.beans.factory.annotation.Autowired
class BaseService {
@Autowired(required = true)
UtilService utilService
}
import grails.test.mixin.*
import org.junit.*
@TestFor(UserService)
class UserServiceTests {
void testSomething() {
fail "Implement me"
}
}
在运行$grails测试应用程序时,我发现以下错误
| Failure: testSomething(com.krish.web.service.UserServiceTests)
| org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exce
ption is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.krish.web.service.UtilService ProcurementBase
Service.utilService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krish.web.ser
vice.UtilService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.spring
framework.beans.factory.annotation.Autowired(required=true)}
at grails.test.mixin.services.ServiceUnitTestMixin.mockService(ServiceUnitTestMixin.groovy:54)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.krish.web.service.UtilService Ba
seService.utilService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krish.web.s
ervice.UtilService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.spri
ngframework.beans.factory.annotation.Autowired(required=true)}
... 1 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krsih.web.service.UtilService] found for de
pendency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annota
tion.Autowired(required=true)}
grails版本是2.1.0
有人能帮助我们为单元测试连接bean吗?单元测试被剥夺了依赖注入。您可以通过添加
@Mock(UtilService)
在类级别的测试类中。现在可能太晚了,但我遇到了同样的问题。问题是您在Grails服务中使用了
@Autowired
。在Grails服务(或控制器)中,如果bean名称与成员变量名称匹配,则在启动时将自动注入您定义的每个成员
如果使用def
,它将只匹配beanName,如果声明类型,它也将匹配类。根据您的IDE支持和编程风格,最好使用def
或显式类型
我假设如果使用@Autowired
,框架必须在应用程序上下文中找到bean,否则会引发异常。在单元测试环境中,Grails默认bean和resources.groovy中的bean不会在启动时添加。框架尝试自动连接bean,但在测试上下文中找不到。由于您使用的是@Autowired
,因此会引发异常
在服务或控制器中注入bean时,请省略@Autowired
。然后它也将在单元测试环境中启动。但是请注意,bean仍然不存在,您只是没有得到异常。因此,所有注入的成员都将为null,您必须在单元测试中使用defineBeans
closure手动设置它
但是,在grails app
(例如src/groovy
)的类之外进行注入时,必须始终使用@Autowired
进行注入。就我而言,在这里,当进行单元测试时,它不会引起问题。这些定制bean还需要在单元测试环境中使用defineBeans
进行设置
Grails版本:2.4.2
Java版本:1.8_31很抱歉,我遇到了一些非常糟糕的问题。一旦我有空,我一定会(如果还没有人)调查此事。dmahapatro你能帮我一下吗,至少让我看看grails是如何连接bean的,然后我就可以破解连接了?