Grails3控制器集成测试用例失败:找不到线程绑定请求 [英] Grails3 controller integration test case fail: No thread-bound request found
本文介绍了Grails3控制器集成测试用例失败:找不到线程绑定请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Integration
@Rollback $ b $ class TestControllerSpec extends Specification {
def (){
}
def cleanup(){
}
voidtest something(){
setup:
def c = new TestController()
c.index()
expect:
c.response.contentType!= null
}
}
获得以下异常
java.lang.IllegalStateException:找不到线程绑定请求:您是否在实际Web请求之外引用请求属性,或者在原始接收线程之外处理请求?如果实际上在Web请求中运行并仍然收到此消息,则代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。在org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes
(RequestContextHolder.java:131)
。在grails.web.api.WebAttributes $ $性状Helper.currentRequestAttributes(WebAttributes.groovy:45)
。在grails.web.api.ServletAttributes $ $性状Helper.getRequest(ServletAttributes.groovy:42)
解决方案
我一直这样做,它似乎工作正常:
添加字段:
@Autowired
WebApplicationContext ctx
在 setup()
:
GrailsWebMockUtil.bindMockWebRequest(ctx)
在 cleanup()
:
RequestContextHolder.resetRequestAttributes()
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)
解决方案
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()
这篇关于Grails3控制器集成测试用例失败:找不到线程绑定请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文