Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails3 controller integration test case fail: No thread-bound request found

With just simple following controller action spock integration-test. Here is my Test.

@Integration
@Rollback
class TestControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
       setup:
        def c = new TestController()
        c.index()
        expect:
        c.response.contentType !=null
    }
}

getting following Exception

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at grails.web.api.WebAttributes$Trait$Helper.currentRequestAttributes(WebAttributes.groovy:45)
    at grails.web.api.ServletAttributes$Trait$Helper.getRequest(ServletAttributes.groovy:42)
like image 455
user1071671 Avatar asked Jul 04 '15 13:07

user1071671


1 Answers

I've been doing this and it seems to work fine:

Add field:

@Autowired
WebApplicationContext ctx

In setup(): GrailsWebMockUtil.bindMockWebRequest(ctx)

In cleanup(): RequestContextHolder.resetRequestAttributes()

like image 86
HypeMK Avatar answered Sep 17 '22 23:09

HypeMK