title: Spring MockMvc doesn't contain cookies
body: <p>I have a controller protected with HTTP Basic authentication.</p>
  
  <p>I setup the app to use session cookies and it works.</p>
  
  <p>However when I test the controller using MockMvc, a successful authentication does not give me any cookie.</p>
  
  <p>Web configuration:</p>
  
  <pre class="lang-java prettyprint-override"><code>package world.pyb.spring.cookiesdemo;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpMethod;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
  import org.springframework.security.config.http.SessionCreationPolicy;
  
  @Configuration
  @EnableWebMvcSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.inMemoryAuthentication().withUser("admin").password("argentina").roles("USER");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          //@formatter:off
          http.httpBasic()
                  .and().authorizeRequests().antMatchers(HttpMethod.GET, "/hello").authenticated()
                  .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
          //@formatter:on
      }
  }
  </code></pre>
  
  <p>Simple controller:
  </p>
  
  <pre><code>package world.pyb.spring.cookiesdemo;
  
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class HelloController {
  
      @RequestMapping("/hello")
      public String index() {
          return "Greetings from Spring Boot!";
      }
  
  }
  </code></pre>
  
  <p>Simple controller test that doesn't give me the session cookie:
  </p>
  
  <pre><code>package world.pyb.spring.cookiesdemo;
  
  import static org.hamcrest.Matchers.equalTo;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  @AutoConfigureMockMvc
  public class HelloControllerTest {
  
      @Autowired
      private MockMvc mvc;
  
      @Test
      public void getHello() throws Exception {
          mvc.perform(MockMvcRequestBuilders.get("/hello")
              .with(SecurityMockMvcRequestPostProcessors.httpBasic("admin", "argentina"))
      )
          // prints "Cookies = []"
          .andDo(MockMvcResultHandlers.print())
          .andExpect(cookie().exists("JSESSIONID"))
          .andExpect(status().is2xxSuccessful())
          .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
      }
  
  }
  </code></pre>
  
  <p>Related questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/26142631/why-does-spring-mockmvc-result-not-contain-a-cookie">Why does Spring MockMvc result not contain a cookie?</a></li>
  <li><a href="https://stackoverflow.com/questions/28231336/unit-testing-login-in-spring-mvc-using-mockmvc">Unit Testing /login in Spring MVC using MockMvc</a></li>
  <li><a href="https://stackoverflow.com/questions/36600817/spring-mvc-testing-security-integration-test-jsessionid-is-not-present">Spring MVC testing (security Integration test), JSESSIONID is not present</a></li>
  </ul>
  
  <p>Some answers suggest not to use MockMvc but I'd like to keep using it if possible.</p>