Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.
An expert guide for testing Spring Boot 4 applications with modern patterns using JUnit 6 and AssertJ. It is organized around the test pyramid - fast unit tests, focused slice tests, and complete integration tests - and the principle of choosing the narrowest slice that still gives confidence. A decision matrix maps scenarios to annotations: @WebMvcTest for controllers and HTTP semantics, @DataJpaTest for repositories and JPA queries (with Testcontainers against a real database), @RestClientTest for REST clients and external APIs, @JsonTest for JSON serialization, and @SpringBootTest for full-application tests, while plain JUnit plus Mockito covers business logic in services. The guide favors modern APIs introduced or highlighted in Spring Boot 4 - MockMvcTester for AssertJ-style web assertions, RestTestClient as the successor to TestRestTemplate, and @MockitoBean replacing the deprecated @MockBean - and points to detailed references for each tool, for AssertJ basics and collections, Testcontainers, Instancio-based test-data generation, context caching, and Spring Boot 4.0 migration. Best-practice sections address code complexity (recommending refactoring when a single method needs more than five to seven test cases), reducing redundancy with helper methods, clarifying intent with @DisplayName, and structuring tests in a consistent order - happy path first, then alternative paths and edge cases, then exceptions and error conditions. It advises writing tests around realistic production scenarios and targeting roughly 80% coverage as a practical balance, tracked with the Jacoco Maven plugin, while prioritizing business-critical paths, complex algorithms, error handling, and integration points. Required test dependencies for Spring Boot 4, including the web-mvc test starter and the Testcontainers starter, are listed so a project can adopt the recommended stack quickly.
Use @WebMvcTest with MockMvcTester for the web layer and HTTP semantics.
@MockitoBean replaces the deprecated @MockBean for mocking dependencies.
Use @DataJpaTest together with Testcontainers so the queries run against a real database rather than an in-memory substitute.
Aim for about 80% coverage as a practical balance, tracked with the Jacoco Maven plugin, while prioritizing business-critical paths, complex algorithms, and error handling.
Main scenario (the happy path) first, then other valid paths and edge cases, and finally exceptions and error conditions.
Quick Setup:
.claude/skills/Repository
github/awesome-copilot