DEV Community

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

Posted on

7

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

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up