gpt4 book ai didi

grails - 从集成测试中的服务访问 servletContext

转载 作者:行者123 更新时间:2023-12-02 10:03:23 25 4
gpt4 key购买 nike

我正在尝试从服务访问servletContext(应用程序上下文)到集成测试中。

以下是我尝试让它进入我的集成测试的方法:

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH 

class ScraperServiceIntegrationTests extends GroovyTestCase {
ScraperService scraperService

def testStoring() {
scraperService = new ScraperService()
scraperService.servletContext = new SCH()
scraperService.storing()
...
}
...
}

以下是我在服务中使用 servlet 上下文的方式:

class ScraperService {

static transactional = true
def servletContext

synchronized def storing() {
servletContext.numberOfCreditProvider = "whatever"
...
}
...
}

我收到以下错误消息:

No such property: numberOfCreditProvider for class: org.codehaus.groovy.grails.web.context.ServletContextHolder

如何解决这个错误?

最佳答案

您将测试中的 servletContext 分配给 ServletContextHolder,而不是实际的上下文本身。

您可能希望在测试中这样做:

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

def testStoring() {
scraperService = new ScraperService()
scraperService.servletContext = SCH.servletContext
scraperService.storing()
...
}

关于grails - 从集成测试中的服务访问 servletContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191782/

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