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 badges1 Answer
Reset to default 1If 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.
本文标签:
版权声明:本文标题:junit - JWTAuthenticationToken is null when testing controller endpoint in Spring Webflux service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741720750a2394373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论