title: Mock SecurityContextHolder / Authentication always returning null
body: <p>I'm aware this question gets asked a lot, but maybe I have some things that are particular to this. I'm trying to do some integration tests on a Spring Boot application that supports REST (not Spring MVC) and for some reason <code>SecurityContextHolder.getContext().getAuthentication()</code> always returns null, even when using <code>@WithMockUser</code> on the test. I'm not certain if this has to do with using profiles on the configuration classes, but so far we haven't had troubles with this.</p>
  
  <h1>Class</h1>
  
  <pre><code>@Override
  public ResponseEntity&lt;EmployeeDTO&gt; meGet() {
      Principal principal = SecurityContextHolder.getContext().getAuthentication();
      logger.debug("Endpoint called: me({})", principal);
      EmployeeDTO result;
  
      // Get user email from security context
      String email = principal.getName(); // NPE here
  
  // ...
  }
  </code></pre>
  
  <h1>Test</h1>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
          properties = {"eureka.client.enabled:false"})
  @WithMockUser
  @ActiveProfiles(value = "test")
  public class MeControllerTest extends IntegrationSpringBootTest {
  
  @Autowired
  private TestRestTemplate restTemplate;
  
  @MockBean
  private SecurityContext securityContext;
  
  @MockBean
  private Authentication authentication;
  
  @MockBean
  private EmployeeRepository employeeRepository;
  
  @BeforeClass
  public static void setUp() {
  
  }
  
  @Before
  @Override
  public void resetMocks() {
      reset(employeeRepository);
  }
  
  @Test
  public void meGet() throws Exception {
      when(securityContext.getAuthentication()).thenReturn(authentication);
      securityContext.setAuthentication(authentication);
      when(authentication.getPrincipal()).thenReturn(mockEmployee());
      SecurityContextHolder.setContext(securityContext);
      when(employeeRepository.findByEmail(anyString())).thenReturn(mockEmployee());
  
      ResponseEntity&lt;EmployeeDTO&gt; employeeDTOResponseEntity =
              this.restTemplate.getForEntity("/me", EmployeeDTO.class);
  // ...
  }
  </code></pre>
  
  <p>If I return a mock <code>Principal</code> instead of <code>mockEmployee()</code> the test cannot even start because this happens:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Could not inject field: private org.springframework.security.core.Authentication com.gft.employee.controller.MeControllerTest.authentication; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'org.springframework.security.core.Authentication#0' is expected to be of type 'org.springframework.security.core.Authentication' but was actually of type '$java.security.Principal$$EnhancerByMockitoWithCGLIB$$657040e6'
  </code></pre>
  
  <p>Additional clarifications: This Spring Boot app also uses OAuth2 for authorization, but it must be turned off for these tests. That's why we use profiles. Omitting the <code>@ActiveProfiles</code> annotation gives us a 401 Unauthorized error against the endpoint request.</p>
  
  <p>I <em>could</em> use PowerMock but I would like to avoid it if possible.</p>