Showing posts with label Request For Background Thread. Show all posts
Showing posts with label Request For Background Thread. Show all posts

Saturday, July 5, 2014

Request Mocking In Grails For Back-end/Background Threads


import javax.servlet.http.HttpServletRequest
import org.springframework.web.context.request.RequestContextHolder
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.springframework.web.context.support.WebApplicationContextUtils

def getRequest() {
    def webRequest = RequestContextHolder.getRequestAttributes();
    if(!webRequest) {
        def servletContext  = ServletContextHolder.getServletContext();
        def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        webRequest = grails.util.GrailsWebUtil.bindMockWebRequest(applicationContext);
    }
    return webRequest.request;
}


This code can broke while working on a WAR environment. The problem was that the 
MockWebRequest class was part of the org.springframework:org.springframework.test:3.0.3.RELEASE jar 
and had to be included in the BuildConfig.groovy as

dependencies {
    runtime 'org.springframework:org.springframework.test:3.0.3.RELEASE'
}

Ensure that the line:

mavenCentral()


is not commented in BuildConfig.groovy

https://drive.google.com/file/d/0B5nZNPW48dpFd3U2UG9DMmVsX28/edit?usp=sharing