Skip to content

Backend Testing

Maria Martinez edited this page Sep 21, 2026 · 2 revisions

Backend Testing Guide

The backend test suite ensures reliability across reactive endpoints, business service logic, data persistence, and external service communication using JUnit, Mockito, Project Reactor StepVerifier, and Testcontainers.


Test Categories

  1. Unit Tests: Fast, isolated tests mocking dependencies via Mockito.
  2. Reactive Stream Tests: Verify non-blocking publishers (Mono and Flux) using reactor.test.StepVerifier.
  3. Integration Tests: Spin up ephemeral PostgreSQL and Oracle containers via Testcontainers to validate R2DBC repositories and Flyway migrations against actual database engines.

Running Backend Tests

# Run unit tests and Testcontainers integration tests
cd backend
mvn clean verify -P all-tests

cd legacy
mvn clean verify -P all-tests

cd processor
mvn clean verify -P all-tests

Note

Running mvn clean verify -P all-tests executes both unit tests and Testcontainers integration tests. To run only fast unit tests without starting Docker containers, execute mvn clean test.


Testing Reactive Streams with StepVerifier

Because Spring WebFlux operates asynchronously, always assert publishers using StepVerifier:

@Test
void shouldRetrieveClientDetails() {
    Mono<ClientDetailsDto> clientMono = clientService.getClientByNumber("00012345");

    StepVerifier.create(clientMono)
        .assertNext(client -> {
            assertNotNull(client);
            assertEquals("00012345", client.clientNumber());
            assertEquals("ACTIVE", client.clientStatusCode());
        })
        .verifyComplete();
}

Integration Testing with Testcontainers

Integration tests bootstrap true PostgreSQL instances using @Testcontainers:

@SpringBootTest
@Testcontainers
class SubmissionRepositoryIntegrationTest {

    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13.23")
        .withDatabaseName("testdb")
        .withUsername("testuser")
        .withPassword("testpass");

    @Autowired
    private SubmissionRepository repository;

    @Test
    void shouldSaveAndRetrieveSubmission() {
        SubmissionEntity entity = new SubmissionEntity();
        entity.setName("Acme Forestry Corp.");

        StepVerifier.create(repository.save(entity))
            .assertNext(saved -> assertNotNull(saved.getSubmissionId()))
            .verifyComplete();
    }
}

Clone this wiki locally