Interested in reactive programming, I played a bit with the Building a Reactive RESTful Web Service guide. And wanted to move forwrad and add some unit tests.
I tried to test my handler (RouterFunction
) with plain Junit/Mockito test. But, because it is reactive, the handler returns a Mono<ServerResponse>
. So I had to block()
it to test the ServerResponse
status but was not able to extract his body to test it. While searching the web for a solution, it seems that all the samples are using the WebTestClient
.
My question is:
Given that all samples are using WebTestClient
to test a reactive REST service and that it is not easier to (unit) test the body of a ServerResponse
. Is it a good practice to unit test a RouterFunction
or is it better to always do broader tests with the WebTestClient
?
Thanks a lot.
A Flux object represents a reactive sequence of 0.. N items, whereas a Mono object represents a single value or an empty (0..1) result. Most times, you expect exactly one result or no (zero) result, and not a collection that contains possibly multiple results. In such scenarios, it's more convenient to have a Mono.
Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types: Mono: implements Publisher and returns 0 or 1 elements. Flux: implements Publisher and returns N elements.
Learn to unit test spring boot webflux controller using @WebFluxTest annotation and WebTestClient which is used to test webflux endpoints with Junit 5.
Blocking way to get value from Mono WebFlux. You can use the block() method to block the current thread indefinitely and wait for Mono to complete. If the Mono completes, then this method either returns the original value or null (Mono is empty). In case of any errors, then the exception is rethrown.
It seems that the best practice 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.-- https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html#web-reactive-tests
It depends on what exactly you want to test, for regular method you should use StepVerifier in favor of consume the output of you test. However router functions are unique, because, usually return a serverResponse that contains your data on body, so in that case its better use webClient or webTestClient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With