Getting httpServletRequest attribute with MockMvc : <p>I have a really simple controller defined in this way:</p>

<pre><code>@RequestMapping(value = "/api/test", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Object getObject(HttpServletRequest req, HttpServletResponse res) {
    Object userId = req.getAttribute("userId");
    if (userId == null){
        res.setStatus(HttpStatus.BAD_REQUEST.value());
    }
    [....]
}
</code></pre>

<p>I tried to call using MockMvc in many different way but, I'm not able to provide the attribute "userId". </p>

<p>For instance, with this it doesn't work:</p>

<pre><code>MockHttpSession mockHttpSession = new MockHttpSession(); 
mockHttpSession.setAttribute("userId", "TESTUSER");             
mockMvc.perform(get("/api/test").session(mockHttpSession)).andExpect(status().is(200)).andReturn(); 
</code></pre>

<p>I also tried this, but without success:</p>

<pre><code>MvcResult result = mockMvc.perform(get("/api/test").with(new RequestPostProcessor() {
     public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
           request.setParameter("userId", "testUserId");
           request.setRemoteUser("TESTUSER");
           return request;
     }
})).andExpect(status().is(200)).andReturn(); 
</code></pre>

<p>In this case, I can set the RemoteUser but never the Attributes map on HttpServletRequest. </p>

<p>Any clue?</p>
