DEV Community

Cover image for Spring Webflux testing with Mockito
Piyush Asutkar
Piyush Asutkar

Posted on

Spring Webflux testing with Mockito

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
  • Mockito

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.

Test Case:
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.

Further reading:

Top comments (0)