admin管理员组

文章数量:1302957

I am fairly new to Spring Webflux and have ran into an issue when trying to unit test the following controller method:

@RestController
@RequestMapping("/tokenService")
@RequiredArgsConstructor
@Slf4j
public class TokenController {

   @GetMapping
   @ResponseBody
   public Mono<ResponseEntity<String>> getTokenValue(final JwtAuthenticationToken auth) {
      return Mono.just(ResponseEntity.ok(auth.getToken().getTokenValue()));
   }
}

For testing the controller method I am using WebTestClient and my unit test class looks as follows:

@ExtendWith(MockitoExtension.class)
public class TokenControllerTest {

    private WebTestClient webTestClient;

    @BeforeEach
    public void setup() {
        webTestClient = WebTestClient.bindToController(new TokenController())
                .build();
    }

    @Test
    public void testGetTokenValueFromEndpoint() throws Exception {
         webTestClient
                .get()
                .uri("/tokenService")
                .exchange()
                .expectStatus().isOk();
    }
}

However, when running the test, I end up seeing a NullPointerException because the auth object is empty. Now if this was an MVC project I know that I could do the following:

mockMvc.perform(get("/tokenService").principal(jwtAuthenticationToken)

I am trying to achieve the same kind of behaviour using using WebTestClient. Any help would be appreciated. Thanks in advance.

I am fairly new to Spring Webflux and have ran into an issue when trying to unit test the following controller method:

@RestController
@RequestMapping("/tokenService")
@RequiredArgsConstructor
@Slf4j
public class TokenController {

   @GetMapping
   @ResponseBody
   public Mono<ResponseEntity<String>> getTokenValue(final JwtAuthenticationToken auth) {
      return Mono.just(ResponseEntity.ok(auth.getToken().getTokenValue()));
   }
}

For testing the controller method I am using WebTestClient and my unit test class looks as follows:

@ExtendWith(MockitoExtension.class)
public class TokenControllerTest {

    private WebTestClient webTestClient;

    @BeforeEach
    public void setup() {
        webTestClient = WebTestClient.bindToController(new TokenController())
                .build();
    }

    @Test
    public void testGetTokenValueFromEndpoint() throws Exception {
         webTestClient
                .get()
                .uri("/tokenService")
                .exchange()
                .expectStatus().isOk();
    }
}

However, when running the test, I end up seeing a NullPointerException because the auth object is empty. Now if this was an MVC project I know that I could do the following:

mockMvc.perform(get("/tokenService").principal(jwtAuthenticationToken)

I am trying to achieve the same kind of behaviour using using WebTestClient. Any help would be appreciated. Thanks in advance.

Share Improve this question asked Feb 10 at 10:32 HeediBoyHeediBoy 458 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If we follow this part of the docs: Auto-configured Spring WebFlux Tests, test can be written as shown below. I added a mock ReactiveJwtDecoder in order to control token handling.

@WebFluxTest(TokenController.class)
class TokenControllerTest {

    @Autowired
    WebTestClient webClient;

    @TestConfiguration
    static class TestConfig {
        @Bean
        public ReactiveJwtDecoder jwtDecoder() {
            return token -> Mono.just(
                    Jwt.withTokenValue(token)
                            .header("alg", "none")
                            .claim("sub", "user")
                            .issuedAt(Instant.now())
                            .expiresAt(Instant.now().plusSeconds(5))
                            .build()
            );
        }
    }

    @Test
    void testGetTokenValue() {
        webClient.get()
                .uri("/tokenService")
                .header(HttpHeaders.AUTHORIZATION, "Bearer mock-token")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class).isEqualTo("mock-token");
    }
}

Tested with Spring Boot 3.4.2.

本文标签: