在集成测试中从服务访问servletContext

3

我正在尝试从服务中的集成测试访问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

我该如何解决这个错误?
2个回答

6

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

您可能希望在测试中使用以下内容:

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

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

就是这样了。非常感谢你们的帮助。 - Alexandre Bourlier

4
org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接