title: Spring Security Unit Test With Csrf causes HttpServletResponse to not be found
body: <p>I was unable to find this question asked anywhere else.</p>
  
  <p>I have my unit test setup as follows: </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  //@DatabaseSetup("classpath:decks.xml")
  @WebIntegrationTest
  public class DeckControllerTest {
      @Autowired
      private WebApplicationContext context;
  
      private MockMvc mockMvc;
  
      @Before
      public void setup() {
          mockMvc = MockMvcBuilders
                  .webAppContextSetup(context)
                  .apply(springSecurity())
                  .build();
      }
  
      @Test
      @WithMockUser
      public void addDeck_ShouldRedirectIfUserLoggedInAndPostsInvalidDeck() throws Exception {
          DeckbuilderForm form = new DeckbuilderForm();
          ObjectMapper mapper = new ObjectMapper();
          String queryString = mapper.convertValue(form, UriFormat.class).toString();
          mockMvc.perform(post("/decks")
              .with(csrf())
              .contentType(MediaType.ALL)
              .content(queryString)
          )
                  .andExpect(status().is3xxRedirection())
                  .andExpect(flash().attributeExists("flash"));
      }
  
      @Test
      @WithMockUser
      public void addDeck_ShouldBeOkWithValidInput() throws Exception {
          DeckbuilderForm form = sampleValidDeckbuilderForm();
          ObjectMapper mapper = new ObjectMapper();
          String queryString = mapper.convertValue(form, UriFormat.class).toString();
  
          mockMvc.perform(post("/decks")
              .with(csrf().useInvalidToken())
              .content(queryString)
          )
                  .andExpect(status().is3xxRedirection());
      }
  }
  </code></pre>
  
  <p>I receive the following stacktrace upon running the test:</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: The HttpServletRequest attribute
    must contain an HttpServletResponse for the attribute
    javax.servlet.http.HttpServletResponse</p>
  </blockquote>
  
  <p>This comes from the class LazyCsrfTokenRepository:</p>
  
  <pre><code>private HttpServletResponse getResponse(HttpServletRequest request) {
      HttpServletResponse response = (HttpServletResponse) request
              .getAttribute(HTTP_RESPONSE_ATTR);
      if (response == null) {
          throw new IllegalArgumentException(
                  "The HttpServletRequest attribute must contain an HttpServletResponse for the attribute "
                          + HTTP_RESPONSE_ATTR);
      }
      return response;
  }
  </code></pre>
  
  <p>Any suggestions to resolve this problem would be highly appreciated, I understand that normally one would use @ContextConfiguration instead of @SpringApplicationConfiguration but I am only able to use this method since I am using Java Config and would not like to duplicate my configuration for tests. </p>