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.
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
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
.sdkmanrcfile for automatic version switching
- You can use SDKMAN! to install Java:
- Maven 3.6+ (wrapper included in the project)
- Docker (for running PostgreSQL via Docker Compose and Testcontainers during tests)
- Git (for cloning the repository)
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
The project uses Maven as the build tool. You can use the Maven wrapper included in the project:
./mvnw compile./mvnw package -DskipTests./mvnw verifyThe project includes Spring Boot Docker Compose support. Simply start the application, and Docker Compose will automatically start the PostgreSQL database:
./mvnw spring-boot:runThe PostgreSQL database will be started automatically using the configuration from compose.yml.
Alternatively, you can start PostgreSQL manually using Docker Compose:
docker compose up -dThen run the application:
./mvnw spring-boot:runThe project includes comprehensive test coverage with both unit and integration tests.
./mvnw verify./mvnw test./mvnw failsafe:integration-test- Unit Tests: Use Surefire plugin with parallel execution enabled
- Integration Tests: Use Failsafe plugin and follow the naming convention
*IT.javaor*WT.java - Testcontainers: Automatically starts PostgreSQL containers for integration tests
- Context Caching: Examples included to optimize Spring context reuse across tests
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.xmlorganized./mvnw sortpom:verify
-
Maven Enforcer: Validates dependency convergence
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
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!
