DEV Community

loading...
Cover image for Spring Webflux testing with Mockito

Spring Webflux testing with Mockito

asutkarpeeyush profile image Piyush Asutkar ・1 min read

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).

Pre-requisites:

  • 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:
https://spring.getdocs.org/en-US/spring-framework-docs/docs/testing/integration-testing/webtestclient.html

Discussion (0)

pic
Editor guide