Skip to content

rieckpil/testing-spring-boot-applications-demystified

Repository files navigation

Testing Spring Boot Applications Demystified

Testing Spring Boot Applications Demystified Cover

This repository contains the source code for the book Testing Spring Boot Applications Demystified.

Spring Boot testing doesn't have to be a nightmare of broken configurations and unreliable tests.

This book transforms testing from your biggest frustration into your secret weapon, with proven strategies that work in real projects and examples you can implement today.

Demystify testing real-world Spring Boot applications to deliver robust & maintainable code with confidence.

Get the Missing Spring Boot Testing Manual for Free

Write code that works, scale without breaking, and deploy daily.

  • Stop guessing: Learn to write tests that actually catch bugs and make you more productive.
  • Sleep better: Eliminate deployment anxiety and refactor fearlessly.
  • Ship faster: Copy-paste recipes for JUnit, Testcontainers, WireMock and Spring Boot.

Sign up below to get access to your 120+ Pages Manual for free 👇🏻

Testing Spring Boot Applications Demystified

Requirements

Before you begin, ensure you have the following installed:

  • Java 21
    • You can use SDKMAN! to install Java: sdk install java 21.0.6-tem
    • The project includes a .sdkmanrc file for automatic version switching
  • Maven 3.6+ (wrapper included in the project)
  • Docker (for running PostgreSQL via Docker Compose and Testcontainers during tests)
  • Git (for cloning the repository)

Technologies Used

This project demonstrates testing techniques for a Spring Boot application that includes:

  • Spring Boot 3 with Java 21
  • Spring Data JPA with PostgreSQL
  • Spring Security for authentication and authorization
  • Spring WebFlux for reactive HTTP clients
  • Thymeleaf for server-side templating
  • Flyway for database migrations
  • Testcontainers for integration testing
  • WireMock for HTTP client mocking
  • JUnit 5 as the testing framework
  • AssertJ for fluent assertions

Building the Application

The project uses Maven as the build tool. You can use the Maven wrapper included in the project:

Compile the project

./mvnw compile

Build the application (skip tests)

./mvnw package -DskipTests

Build the application (with tests)

./mvnw verify

Running the Application

Using Docker Compose

The project includes Spring Boot Docker Compose support. Simply start the application, and Docker Compose will automatically start the PostgreSQL database:

./mvnw spring-boot:run

The PostgreSQL database will be started automatically using the configuration from compose.yml.

Manual Database Setup

Alternatively, you can start PostgreSQL manually using Docker Compose:

docker compose up -d

Then run the application:

./mvnw spring-boot:run

Running Tests

The project includes comprehensive test coverage with both unit and integration tests.

Run all tests

./mvnw verify

Run only unit tests

./mvnw test

Run only integration tests

./mvnw failsafe:integration-test

Test Configuration

  • Unit Tests: Use Surefire plugin with parallel execution enabled
  • Integration Tests: Use Failsafe plugin and follow the naming convention *IT.java or *WT.java
  • Testcontainers: Automatically starts PostgreSQL containers for integration tests
  • Context Caching: Examples included to optimize Spring context reuse across tests

Code Quality

The project enforces code quality standards:

  • Spotless: Ensures consistent code formatting using Google Java Format

    ./mvnw spotless:check
    ./mvnw spotless:apply
  • SortPOM: Keeps the pom.xml organized

    ./mvnw sortpom:verify
  • Maven Enforcer: Validates dependency convergence

Key Testing Patterns Demonstrated

This codebase showcases various testing patterns covered in the book:

  • Unit testing with JUnit 5 and Mockito
  • Integration testing with @SpringBootTest
  • Testing Spring Data JPA repositories
  • Testing REST controllers with MockMvc
  • Testing reactive WebClient with WireMock
  • Spring Security testing techniques
  • Optimizing Spring context caching
  • Parallel test execution
  • Testcontainers for realistic integration tests

License

The code in this repository is available for educational purposes as companion material for the book.

Testing is a team sport, so feel free to share the link to get the eBook copy of Testing Spring Boot Applications Demystified with your colleagues.


Joyful Testing!

About

🧪 The Missing Spring Boot Testing Manual - Write code that works, scale without breaking, and deploy daily.

Topics

Resources

License

Stars

Watchers

Forks