gpt4 book ai didi

grails - 将来自 resources.groovy 的 Spring Bean 注入(inject) Grails 3 服务的集成测试中

转载 作者:行者123 更新时间:2023-12-02 14:14:00 24 4
gpt4 key购买 nike

我正在尝试为注入(inject)了 spring bean 的服务编写集成测试。 spring bean 定义在 resources.groovy .我的服务正在使用的 bean 似乎没有在我的集成测试中被注入(inject),但是当我运行 grails run-app 时它被很好地注入(inject)了。 .

这是一个最小的失败示例:
grails-app/conf/spring/resources.groovy

beans = {
myBean(Object){}
}
grails-app/services/MyService.groovy
class MyService {
def myBean

def serviceMethod(){
myBean.class.simpleName
}
}
grails-app/src/integration-test/groovy/MyServiceSpec.groovy
@Integration
class MyServiceSpec extends Specification {
def myService

when:
def myBean = myService.myBean

then:
myBean != null
}

Grails 版本信息:
$ grails -v
| Grails Version: 3.1.9
| Groovy Version: 2.4.7
| JVM Version: 1.8.0_92

更新:

Spring 似乎可以很好地注入(inject)其他服务。如果我在 MyService 中声明另一个服务,它被注入(inject)。
class MyService {
def myBean
def myOtherService

def serviceMethod(){
myBean.class.simpleName
}
}

最佳答案

我可能迟到了……但是,对于 future 的读者,请尝试设置 grails.gorm.autowiretrue在您的 application.yml

关于grails - 将来自 resources.groovy 的 Spring Bean 注入(inject) Grails 3 服务的集成测试中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648134/

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