gpt4 book ai didi

grails - 如何自动将在 resources.groovy 中声明的 Grails 组件注入(inject)到我的 IntegrationSpecs 中?

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

使用 Spock (0.7) Grails (2.1.2) 插件,您可以编写自动注入(inject) Grails 原型(prototype)(如服务)的集成规范。但是,我想对仅在我的 resources.groovy 中声明的 Spring bean 做同样的事情。 .例如:

beans = {
simpleBean(SimpleBean) {
// load some dependencies
}
}

src/groovy 中声明 SimpleBean 的地方文件夹。如果这是一个 Grails 服务,我可以在我的 test/integration 中写如下内容文件夹::
import grails.plugin.spock.IntegrationSpec

class SimpleBeanSpec extends IntegrationSpec {

def simpleBean

def "should"() {
when:
data = simpleBean.load()
then:
assert simpleBean
}

}

但是上面抛出了 NullPointerException调用 simpleBean.load() .有什么方法可以让 Spock/Grails 创建 simpleBean 依赖项,以便它拥有来自 resources.groovy 的所有配置依赖项就像 Grails 服务一样?

最佳答案

Grails 自动将所有 bean 注入(inject)到集成测试中,包括 Spock 测试。 resources.conf 中声明的 Bean应该与人工制品一样工作。您的 IntegrationSpecs 是否位于 test/integration 下? ?

关于grails - 如何自动将在 resources.groovy 中声明的 Grails 组件注入(inject)到我的 IntegrationSpecs 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14364436/

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