title: Autowired HttpServletRequest in Spring-test integration tests
body: <p>I am trying to do a test to cover login functionality. Version of Spring is 3.2.12. I have a session bean, declared as:</p>
  
  <pre><code>@Service
  @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
  public class ClientSessionServiceImpl implements ClientSessionService {
      @Autowired
      private HttpServletRequest request;
      // This method is called during the login routine from the filter
      public boolean checkUser() {
      // I rely on request attributes here, which were set in the filter
      }
  </code></pre>
  
  <p>This works perfectly when run on the server, but when run with the means of spring-test, the problem comes. This is my test method:</p>
  
  <pre><code>this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilter(springSecurityFilterChain).build();
  mockMvc.perform(post(URL));
  </code></pre>
  
  <p>After debugging, i found out, that when test spring context is started, in the
      ServletTestExecutionListener.setUpRequestContextIfNecessary 
  an instance of MockHttpServletRequest is created,
      MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); 
      // Lets' call this instance A.
  And this is the instance that's get injected everywhere, i use</p>
  
  <pre><code>@Autowired
  HttpServletRequest request;
  </code></pre>
  
  <p>Whereas, calling MockMvc.perform, creates another instance of MockHttpServletRequest  (Let's call it instance B), which is passed to all the filters, servlets, etc. So, basically, the attribute i set in the filter in the request, can't be read in the ClientSessionServiceImpl, because different instance of MockHttpServletRequest is injected there.</p>
  
  <p>I spent bunch of time on this, but still have not found the solution.</p>
  
  <p>P.S.
  I searched through StackOverflow, there are questions with similar titles, but describing the problems that differ from mine, as i don't want to pass HttpServletRequest as a parameter, and would prefer to have it Autowired, unless there is a good reason for it.</p>