title: Unit testing a method dependent to the request context
body: <p>I'm writing a unit test for a method that contains the following line:</p>
  
  <pre><code>String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>The reason is quite obvious — I'm not running the test in a request context.</p>
  
  <p>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?</p>
  
  <p>Thank you very much.</p>