How it works...

To test RESTful services, aside from using the typical MockMvc, Spring Framework provides a utility class named TestRestTemplate, which is a convenient alternative to Spring REST integration tests. Together with the simulated random ports given by WebEnvironment.RANDOM_PORT of @SpingBootTest, TestRestTemplate can easily be injected into the test class without much configuration and can then proceed executing the REST endpoints through its methods such as exchange(), getForObject(), postForObject(), delete(), and put().

Since there is no utility test class for asynchronous or non-blocking REST services, it is recommended to use MockMvc with some asynchronous mocking events such as asyncStarted() and asyncDispatch(). Although not pretty straightforward as the blocking counterpart, the first request execution of the perform() method of MockMvc with the service URL must execute the asyncStarted() method, which flags the start of asynchronous processing. Then create another request execution triggered by asyncDispatch(), which handles the continuation of the previous request processing. If the test method always returns a null result, set a time delay to the first MvcResult before the second perform() method starts to execute.

Reactive REST services can be tested through WebTestClient, which was discussed in the previous chapter. It is a variant of WebClient that is used to run services that return the Flux<T>, Mono<T>, or Publisher<T> stream data. It is also used to save a stream of data into the repository. The REST endpoint generated by HandlerRouter<T> and HandlerFunction<T> can also be tested using WebTestClient.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.123.73