title: Run unit tests on controllers that require authentication
body: <p>I have a spring boot application which requires login for some actions. I am trying to test them using <code>MockMvc</code>, but it doesn't seem to work. I keep getting a HTTP response with status 403 (forbidden). Probably there is something wrong with the authentication part.</p>
  
  <p>I have tried following the <a href="http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test" rel="noreferrer">documentation</a>, but I wasn't able to get it working.</p>
  
  <p>This is my current testing code:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = {Application.class})
  @WebIntegrationTest("server.port = 8093")
  public class PasswordChangeTests {
      @Autowired
      private EmbeddedWebApplicationContext webApplicationContext;
  
      @Autowired
      private UserRepository userRepository;
  
      private MockMvc mockMvc;
  
      @Before
      public void setUp() throws Exception {
          this.mockMvc = MockMvcBuilders
                  .webAppContextSetup(webApplicationContext)
                  .apply(springSecurity())
                  .build();
      }
  
       @Test
       public void changePasswordWorks() throws Exception {
           // Send password change request
           PasswordChangeRepresentation passwordChange = new PasswordChangeRepresentation(DefaultUsers.Admin.getPassword(), "12345678");
           mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/password/change")
                   .content(new ObjectMapper().writeValueAsString(passwordChange))
                   .contentType(MediaType.APPLICATION_JSON)
                   .accept(MediaType.APPLICATION_JSON))
                   .andExpect(status().isOk());
  
           // Check that the password has been changed
           User user = this.userRepository.findByUsername(DefaultUsers.Admin.getEmail());
           Assert.assertEquals(user.getPassword(), "12345678");
      }
  }
  </code></pre>
  
  <p>Sorry if I am missing something obvious. This is my first experience with spring boot.</p>