Spring-Webflux: How to test controllers?

Reading Time: 3 minutes

Introduction :

While working with Spring MVC, you might have used Spring MockMVC to perform testing of Spring web MVC controllers. MockMVC class is part of the Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. But, this is not something that will work if you are using SpringBoot Webflux. If you have a Spring application built with Webflux, the MVC controllers can be tested using WebTestClient.

To start with, first, we need to have the following dependency in pom.xml.






To understand now, how things will work let us try to consider an example of an Employee CRUD application. I have my EmployeeController like this:



public class EmployeeController {

    private EmployeeService employeeService;

    @GetMapping(value = "/employees/{id}")

    public Mono<Employee> getEmployeeById(@PathVariable("id") int id) {

        return employeeService.getEmployeeById(id);
    @PostMapping(value = "/employees", consumes = "application/json")


    public Mono<Employee> createEmployee(@RequestBody Employee employee) {

        return employeeService.createEmployee(employee);

    @DeleteMapping(value = "/employees/{id}")

    public Mono<String> deleteEmployeeById(@PathVariable("id") int id) {

        return employeeService.deleteEmployeeById(id);

My controller is dependent on EmployeeService for all of its business layer logic.

We need not worry about how things are being done inside EmployeeService because for now, we are writing unit test cases for our EmployeeController.

It’s time to start with EmployeeControllerTest. Let’s write our test cases and then understand, what’s actually happening?



public class EmployeeControllerTest {


    WebTestClient webTestClient;


    private EmployeeService employeeService;

    public void testGetEmployeeById() {

        Employee employee = 


        Mono<Employee> employeeMono = Mono.just(employee);








                .value(employee1 -> employee.getAge(), equalTo(23));


    public void testDeleteEmployeeById() {

with id 1 is deleted."));






                .isEqualTo("Employee with id 1 is deleted.");


    public void testCreateEmployee() {

        Employee employee =

        Mono<Employee> employeeMono = Mono.just(employee);





                .body(Mono.just(employee), Employee.class)




Let’s discuss the concepts used above one by one:

  1. At the start of the test class, we added the @RunWith(SpringRunner.class). This tells JUnit to run using Spring’s testing support.
  2. we have also used @WebFluxTest(EmployeeController.class). Using this annotation will disable full auto-configuration and instead apply only configuration relevant to WebFlux tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, and WebFluxConfigurer beans but not @Component, @Service, or @Repository beans). Typically @WebFluxTest is utilize in combination with @MockBean or @Import to create any collaborators that need your @Controller beans.
  3. Inside our test class, we have our webTestClient Object. It is a non-blocking, reactive client for testing web servers. It uses reactive WebClient internally to perform requests and provides a fluent API to verify responses. WebTestClient can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.
  4. We have used @MockBean annotation with EmployeeService. When @MockBean is used on a field, as well as being registered in the application context, the mock will also be injected into the field.
  5. With WebTestClient we can define the route and its expected response in the test case. The assertions can be done using expectStatus()expectHeader() or even expectBody(). How to use them is already in the code above.

It’s pretty straightforward. Once, the WebTestClinet is enunciating, just use it to define your routes and their responses. This is how we can leverage WebTestClient to test our controllers.

I hope, you have liked my blog. If you have any doubt or any suggestions to make please drop a comment. Thanks!


WebTestClient official Do

Conclusion :

In this article, we learned more about Spring Async and Spring WebFlux, then we had a comparison of them theoretically and practically with a basic load test.

Written by 

He is a Software Consultant at Knoldus Inc. He has done B.Tech from Dr. APJ Kalam Technical University Uttar-Pradesh. He is passionate about his work and having the knowledge of various programming languages like Java, C++, Python. But he is passionate about Java development and curious to learn Java Technologies. He is always impatient and enthusiastic to learn new things. He is good skills of Critical thinking and problem solving and always enjoy to help others. He likes to play outdoor games like Football, Volleyball, Hockey and Kabaddi. Apart from the technology he likes to read scriptures originating in ancient India like Veda,Upanishad,Geeta etc.