Issue
How can I test a handler method that receive ServerRequest
and return Mono<ServerResponse>
?
I can create a ServerRequest
via org.springframework.mock.web.reactive.function.server.MockServerRequest.builder()
. And assert on the ServerResponse.statusCode()
. However I would like to test the body of this ServerResponse
but there is no way to extract it.
ServerResponse response = target.search(MockServerRequest.builder()
.method(HttpMethod.GET)
.build()
).block();
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
//assertThat(response.body()).isNotNull();
I don't want to do a broader test with the WebTestClient
, I would like to test all possible cases of response with unit tests.
Thanks
Solution
So, it seems that the best solution is to use the WebTestClient
. However this one can be used without a running server;
The
spring-test
module includes aWebTestClient
that can be used to test WebFlux server endpoints with or without a running server.
The trick is to use the bindTo..()
builder method. In my case bindToRouterFunction(new MyRouter(new MyHandler()))
works well
Answered By - gervais.b
Answer Checked By - Terry (JavaFixing Volunteer)