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

时间:2011-08-25 14:05:37

标签: grails service integration-testing servlets

我正在尝试从服务访问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 个答案:

答案 0 :(得分:5)

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

你可能在测试中想要这个:

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

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

答案 1 :(得分:4)

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