title: JUnit Test with Spring Security
body: <p>I want to test, that I am not authorized to do that. 
  Here my code:</p>
  
  <pre><code>/* imports */
  
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = Application.class)
  @WebAppConfiguration
  public class AuthenticationTest {
  
  private UsernamePasswordAuthenticationToken authentication;
  
  @Autowired
  private AuthenticationManager authManager;
  
      public void before() throws Exception {
          this.authentication = new UsernamePasswordAuthenticationToken("username", "password");
          SecurityContextHolder.getContext().setAuthentication(manager.authenticate(authentication));
  
      }
  
      @Test(expected = AccessDeniedException.class)
      public void postExperience() throws Exception {
          ExperienceEntity experience = new ExperienceEntity();
          experience.setExperience("Test");
          experience.setExperienceEng("Test");
  
          mockMvc.perform(
                      post(URL_EXPERIENCES).principal(authentication).content(json(experience)).contentType(CONTENT_TYPE))
                      .andExpect(status().isForbidden());
          }
  </code></pre>
  
  <p>Error Log:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.security.access.AccessDeniedException: Access is denied
  </code></pre>
  
  <p>I don't understand why this test don't work. I get these error, which I expected. </p>