Доступ к servletContext из службы в рамках интеграционного теста



Я пытаюсь получить доступ к 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()
...
}
...
}


Вот как я использую контекст сервлета в своей службе:



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


Как я могу решить эту ошибку?

565   2  

2 ответов:

Вы присваиваете свой servletContext в тесте ServletContextHolder вместо самого фактического контекста.

Вы, вероятно, хотите, чтобы это было в вашем тесте:

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

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

Comments

    Ничего не найдено.