Skip to main content

Reactive Programming in Spring WebFlux Series - IX

· 4 min read


Test


Introduction

In previous articles, we explored the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, performance optimization, complex scenarios, and security. This article delves into how to conduct testing in Spring WebFlux to ensure that our reactive applications run reliably in both development and production environments.

Why Testing is Necessary​

Testing is an indispensable part of the software development process. Through testing, we can validate code correctness, ensure individual modules function as expected, and promptly identify and resolve potential issues. For reactive applications like those built with Spring WebFlux, testing is particularly important because reactive programming introduces asynchronous and non-blocking operations, which increase the complexity of debugging and validation.

Types of Testing in Spring WebFlux​

In Spring WebFlux, there are typically three main types of tests:

  • Unit Testing: Testing individual modules or components to ensure their behavior matches expectations.
  • Integration Testing: Testing the interactions between multiple modules to ensure they work together correctly.
  • End-to-End Testing: Simulating real user actions and verifying the behavior of the entire system.

1. Unit Testing​

Unit testing is the smallest test scope, focusing on individual functionality or components. In Spring WebFlux, we commonly test controllers, services, and data access layers.

1.1 Testing Controllers with WebTestClient​

WebTestClient is a powerful tool for testing the web layer of Spring WebFlux applications.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@WebFluxTest(UserHandler.class)
public class UserHandlerTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

This example shows how to test a controller using WebTestClient, focusing on the GET /user/1 and GET /users endpoints.

1.2 Testing Services with Mockito​

Mockito is a popular mocking framework used to create and manage dependencies in tests.

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;

public class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

public UserServiceTest() {
MockitoAnnotations.openMocks(this);
}

@Test
void testGetUser() {
User user = new User("1", "John Doe");
when(userRepository.findById("1")).thenReturn(Mono.just(user));

Mono<User> result = userService.getUser("1");
assertEquals("John Doe", result.block().getName());
}
}

In this example, we use Mockito to mock the UserRepository and verify that the UserService correctly retrieves the user.

2. Integration Testing​

Integration tests validate the interactions between multiple modules. In Spring WebFlux, we usually use the @SpringBootTest annotation to load the entire application context and use WebTestClient to perform tests.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationIntegrationTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

In this example, the entire application is loaded and tested for correctness across modules using WebTestClient.

3. End-to-End Testing​

End-to-end testing simulates real user actions and verifies the behavior of the entire system. These tests are generally more complex as they involve starting the whole application and mimicking various user interaction scenarios.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EndToEndTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testUserFlow() {
// Create a new user
User newUser = new User(null, "Alice");
webTestClient.post().uri("/user")
.bodyValue(newUser)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("Alice");

// Get all users
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(4);
}
}

This example tests the flow of creating a new user and then retrieving all users to ensure the system works end-to-end.

Conclusion​

In this article, we discussed how to perform testing in Spring WebFlux. By conducting unit tests, integration tests, and end-to-end tests, you can ensure that every part of your application functions as expected. Additionally, understanding common vulnerabilities like CSRF and XSS is crucial for building secure applications.