Back to Repositories

Testing Reactive REST Endpoints in spring-boot-examples

This test suite demonstrates unit testing of a Spring WebFlux controller using WebTestClient. It validates the basic functionality of a reactive REST endpoint through non-blocking request handling and response verification.

Test Coverage Overview

The test coverage focuses on verifying the HTTP GET endpoint ‘/hello’ in a reactive Spring WebFlux application.

Key areas tested include:
  • Endpoint availability and response status
  • Basic reactive request-response flow
  • Spring WebFlux controller integration

Implementation Analysis

The testing approach utilizes Spring’s WebFluxTest annotation for focused controller testing without starting a full application context.

Implementation highlights:
  • WebTestClient for reactive endpoint testing
  • Isolated controller testing with @WebFluxTest
  • Non-blocking request verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 test runner
  • SpringRunner for test execution
  • WebTestClient for reactive HTTP requests
  • Spring Boot test autoconfiguration
  • WebFlux test support classes

Best Practices Demonstrated

The test suite exemplifies several testing best practices in reactive applications.

Notable practices include:
  • Focused controller testing with proper isolation
  • Clean test method naming conventions
  • Efficient use of Spring Boot test annotations
  • Reactive endpoint validation patterns

ityouknow/spring-boot-examples

spring-boot-webflux/src/test/java/com/neo/HelloTests.java

            
package com.neo;

import com.neo.web.HelloController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

@RunWith(SpringRunner.class)
@WebFluxTest(controllers = HelloController.class)
public class HelloTests {
    @Autowired
    WebTestClient client;

    @Test
    public void getHello() {
        client.get().uri("/hello").exchange().expectStatus().isOk();
    }
}