title: Spring mvc 3.1 integration tests with session support
body: <p>I'm using the new <a href="http://blog.springsource.org/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework/">spring-test</a> in the 3.1 version to run integration tests. It works really well but I can't make the session to work. My code:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration("src/main/webapp")
  @ContextConfiguration({"classpath:applicationContext-dataSource.xml",
        "classpath:applicationContext.xml",
        "classpath:applicationContext-security-roles.xml",
        "classpath:applicationContext-security-web.xml",
        "classpath:applicationContext-web.xml"})
  public class SpringTestBase {
  
      @Autowired
      private WebApplicationContext wac;
      @Autowired
      private FilterChainProxy springSecurityFilterChain;
      @Autowired
      private SessionFactory sessionFactory;
  
      protected MockMvc mock;
      protected MockHttpSession mockSession;
  
      @Before
      public void setUp() throws Exception {
         initDataSources("dataSource.properties");
  
         mock = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
         mockSession = new MockHttpSession(wac.getServletContext(), UUID.randomUUID().toString());
      }
  
      @Test
      public void testLogin() throws Exception {
          // this controller sets a variable in the session
          mock.perform(get("/")
              .session(mockSession))
              .andExpect(model().attributeExists("csrf"));
  
          // I set another variable here just to be sure
          mockSession.setAttribute(CSRFHandlerInterceptor.CSRF, csrf);
  
          // this call returns 403 instead of 200 because the session is empty...
          mock.perform(post("/setup/language")
              .session(mockSession)
              .param(CSRFHandlerInterceptor.CSRF, csrf)
              .param("language", "de"))
              .andExpect(status().isOk());
      }
  }
  </code></pre>
  
  <p>My session is empty in every request, I don't know why.</p>
  
  <p><strong>EDIT:</strong> The last assert is failing: <code>andExpect(status().isOk());</code>. It returns 403 instead of 200.</p>