【问题标题】:Accessing servletContext from a service within an integration test从集成测试中的服务访问 servletContext
【发布时间】:2011-08-25 14:05:37
【问题描述】:

我正在尝试将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

我该如何解决这个错误?

【问题讨论】:

    标签: grails service integration-testing servlets


    【解决方案1】:

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

    您可能希望在测试中使用这个:

    import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
    
    def testStoring() {
        scraperService = new ScraperService()
        scraperService.servletContext = SCH.servletContext
        scraperService.storing()
        ...
    }
    

    【讨论】:

    • 就是这样。谢谢你们的帮助。
    【解决方案2】:
    org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext()
    

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2018-11-18
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多