I'm writing a unit test for a method that contains the following line:
String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();
I get the following error:
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.
The reason is quite obvious — I'm not running the test in a request context.
The question is, how can I test a method that contains a call to a method dependent to the request context in a test environnment?
Thank you very much.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…