Spring Webflux provides fully non-blocking support for building web applications and Mockito is used to generate mock objects during testing. This article focuses on using Mockito alongside WebTestClient (client to test webflux server endpoints).
- Spring Webflux
A simple Webflux router configuration:
The Handler function that the router invokes:
Note: The router and handler in the webflux world act as rest controllers in the webmvc world.
The Service that the handler invokes:
Note: Ideally, the service would interact with the DAO layer to retrieve the data. Keeping things simple to focus on mocking during the testing.
This test case injects mocks into the handler layer, mocks the service layer and then uses WebTestClient to call the API.
We can also use PowerMockito in a similar way in conjunction with Webflux and Mockito.