Using Testing Frameworks and Tools for Reactive Testing (e.g., WebTestClient)

When developing applications using Spring Web Flux, it is crucial to ensure the reliability and correctness of the reactive code. One of the most effective ways to achieve this is through the use of testing frameworks and tools, such as WebTestClient. In this article, we will explore how to use these tools for testing reactive applications.

What is WebTestClient?

WebTestClient is a testing framework specifically designed for testing Spring Web Flux applications. It provides a high-level API for interacting with your application's endpoints and handling the reactive nature of the responses.

Setting Up WebTestClient

To use WebTestClient, you need to include the spring-boot-starter-webflux dependency in your project's build file. For example, if you are using Maven, you can add the following dependency to your pom.xml file:


After adding the dependency, you can use the @AutoConfigureWebTestClient annotation in your test classes to automatically configure an instance of the WebTestClient for the tests:

public class MyWebFluxTest {
    private WebTestClient webTestClient;

Writing Tests with WebTestClient

Now that you have set up WebTestClient, you can start writing tests for your reactive application.

Sending Requests

To send requests to your application's endpoints, you can use the webTestClient instance created earlier. The WebTestClient provides several methods, such as get(), post(), put(), and delete(), to perform different types of HTTP requests.

For example, to test a GET request to the /users endpoint that returns a list of users, you can write the following test:

public void testGetUsers() {

In this test, we send a GET request to the /users endpoint and expect an HTTP status of 200 (OK). We also expect the response body to be a list of User objects with a size of 2.

Handling Responses

The WebTestClient provides convenient methods to handle the responses returned by your application. You can use methods like expectStatus() to validate the HTTP status code, and expectBodyList() to validate the response body's structure.

Here's an example of testing the response body structure:

public void testGetUserById() {
        .jsonPath("$.name").isEqualTo("John Doe");

In this test, we send a GET request to the /users/1 endpoint and expect an HTTP status of 200 (OK). We also use jsonPath() to validate that the response body has the expected id and name fields.

Testing Reactive Streams

When testing reactive code, it is essential to handle the asynchronous nature of reactive streams correctly. WebTestClient provides various methods, such as returnResult() and returnResultList(), which allow you to handle reactive streams and assert on the resulting data.

For example, if you have an endpoint that returns a Flux<User>, you can test it as follows:

public void testGetUsersStream() {
    FluxExchangeResult<User> result = webTestClient.get().uri("/users/stream")


In this test, we use returnResult(User.class) to obtain the FluxExchangeResult<User> and then use StepVerifier to assert on the contents of the Flux<User>.


Testing reactive applications is crucial to ensure the correctness and reliability of your code. With tools like WebTestClient, you can easily write tests that handle the reactive nature of your endpoints. By incorporating testing frameworks and tools into your development workflow, you can have confidence in the functionality and performance of your Spring Web Flux application.

© NoobToMaster - A 10xcoder company