title: Mocking JWT token in @SpringBootTest with WebTestClient
body: <p>I have the following controller class in a Spring Boot project:</p>
  <pre class="lang-java prettyprint-override"><code>@GetMapping
  public ResponseEntity&lt;UserResponse&gt; getUser(@AuthenticationPrincipal CustomUserDetails userDetails) {
      try {
          final UserResponse userData = userService.getUser(userDetails.getId());
          return ResponseEntity.ok(userData);
      } catch (UserNotFoundException e) {
          log.error(&quot;User with id {} not found&quot;, userDetails.getId());
          return ResponseEntity.notFound().build();
      }
  }
  </code></pre>
  <p>This resource is only accessible if the client sends a JWT token with <code>Authorization: Bearer &lt;token&gt;</code>. The <code>CustomUserDetails</code> are provided by a <code>CustomUserDetailsService</code> after having parsed the JWT token via a <code>JwtRequestFilter</code>.</p>
  <p>Now I'd like to write a <code>@SpringBootTest</code> which uses a real HTTP client calling this resource. My first idea was to use a <code>MockMvc</code> object but then I read about the <code>WebTestClient</code> provided by Spring.</p>
  <p>However I don't yet understand how I would be able to mock the JWT token. This is my initial attempt:</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  @AutoConfigureWebTestClient
  public class UserControllerIT {
  
      @Autowired
      private ApplicationContext context;
  
      private WebTestClient webTestClient;
  
      @MockBean
      private UserRepo userRepo;
  
      @BeforeEach
      public void setup() {
          webTestClient = WebTestClient
                  .bindToApplicationContext(context)
                  .apply(springSecurity())
                  .configureClient()
                  .build();
      }
  
      @Test
      @WithMockUser
      public void someTest() {
          final User user = createUser(&quot;foo@bar.com&quot;, &quot;my-password&quot;);
  
          when(userRepo.findById(anyLong())).thenReturn(Optional.of(user));
  
          webTestClient
                  .mutateWith(mockJwt())
                  .get()
                  .uri(&quot;/user&quot;)
                  .header(ACCEPT, APPLICATION_JSON_VALUE)
                  .exchange()
                  .expectStatus().is2xxSuccessful();
      }
  </code></pre>
  <p>This test fails with the following exception:</p>
  <p><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'webHandler' available</code></p>
  <p>However I'm not sure if my approach makes sense. Is <code>WebTestClient</code> the &quot;correct&quot; way? Or do I need to use a <code>WebClient</code>?</p>
  <p>My security configuration:</p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      private final JwtRequestFilter jwtRequestFilter;
  
      public SecurityConfiguration(JwtRequestFilter jwtRequestFilter) {
          this.jwtRequestFilter = jwtRequestFilter;
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Override
      protected void configure(final HttpSecurity httpSecurity) throws Exception {
          httpSecurity
                  .cors().and()
                  .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class)
                  .mvcMatcher(&quot;/services/**&quot;).authorizeRequests()
                  .mvcMatchers(PUBLIC_RESOURCES).permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
      }
  }
  </code></pre>