title: How to pass Authentication for Thymeleasf template during unit test
body: <p>I am using Spring Boot 2.0.8.RELEASE. I have a controller which has the following method construct </p>
  
  <pre><code>@PostMapping(value = "/profile/change-password", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
      public Mono&lt;String&gt; changePasswordSubmit(Authentication authentication, @RequestBody MultiValueMap&lt;String, String&gt; formData) {
  </code></pre>
  
  <p>And My unit test which looks like:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebFluxTest(controllers = ChangePasswordController.class)
  @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  @Import({ThymeleafAutoConfiguration.class, SpringSecurityContextUtils.class})
  @WithMockUser(username = "test", password = "password")
  public class ChangePasswordControllerTest {
  
      @Autowired
      WebTestClient webTestClient;
      @MockBean
      SpringUserDetailsRepository userDetailsRepository;
  
      @Autowired
      ChangePasswordController controller;
  
      @MockBean
      Authentication authentication;
  
      @Test
      public void addNewEntrySubmit() {
          MultiValueMap&lt;String, String&gt; formData = new LinkedMultiValueMap&lt;&gt;();
          formData.put("password1", Collections.singletonList("password"));
          formData.put("password2", Collections.singletonList("password"));
  
          webTestClient.post().uri("/profile/change-password").contentType(MediaType.APPLICATION_FORM_URLENCODED)
                  .body(BodyInserters.fromFormData(formData)).exchange().expectStatus().isSeeOther().expectHeader().valueEquals(HttpHeaders.LOCATION, "/page/1");
  
  //        verify(userDetailsRepository).updatePassword(captor.capture(), captor.capture());
          doNothing().when(userDetailsRepository).updatePassword(any(), any());
      }
  }
  </code></pre>
  
  <p>My problem is that when I run the test the Authentication value on the controller is null. I have tried to add the security context but am having issues with getting it right. How do I fix this</p>
  
  <p>Update:
  Link to example repository: <a href="https://github.com/dmbeer/thymeleaf-spring-security-test" rel="nofollow noreferrer">https://github.com/dmbeer/thymeleaf-spring-security-test</a></p>