spring-boot-testing

Write unit, slice, and integration tests for Spring Boot REST applications.

39|3|Updated Jul 28, 2025
One-click install
npx skills add https://github.com/mzivkovicdev/spring-crud-generator --skill spring-boot-testing-mzivkovicdev
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: spring-boot-testing
Source: https://github.com/mzivkovicdev/spring-crud-generator/tree/main/.agents/skills/spring-boot-testing
Command: npx skills add https://github.com/mzivkovicdev/spring-crud-generator --skill spring-boot-testing-mzivkovicdev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Spring Boot test suites often mix test levels, mock the wrong boundaries, or silently skip integration coverage, leaving production behavior unproven. This Skill defines a consistent testing standard covering scenario selection, test scope, fixtures, isolation, and execution for Java 21+ Spring Boot 3 and 4 applications. ## Core Features & Use Cases - Layered test strategy: Distinguishes Spring-free unit tests, @WebMvcTest controller slices, @DataJpaTest persistence slices, and full @SpringBootTest integration tests, with rules for what each level must and must not prove. - Real infrastructure integration testing: Enforces Testcontainers with the production database engine, migration-tool-built schemas, deterministic cleanup, and real security filter chains with valid credentials instead of @WithMockUser shortcuts. - Scheduler and generation-aware guidance: Covers scheduled job tests at both unit and integration levels, plus the Spring Boot 3 vs 4 differences in test starters, MockMvc auto-configuration, and RestTestClient vs TestRestTemplate. - Use Case: When adding a new REST endpoint, apply this Skill to create the controller slice test, the service unit tests, and a database-backed integration test that verifies committed state and the error contract. ## Quick Start Use the spring-boot-testing skill to write the required unit, MVC slice, and integration tests for the new UserController registration endpoint.

Frequently Asked Questions about spring-boot-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write integration tests for a Spring Boot REST API?▼

Use @SpringBootTest with MockMvc or a random-port client, send requests through the real controller, service, repository, and security filter chain, and verify both the response and committed database state. Run against the production database engine via Testcontainers, never H2 substitutes.

What is the difference between @WebMvcTest and @SpringBootTest?▼

@WebMvcTest is a focused controller slice with mocked services that proves routing, validation, serialization, and error contracts without the security filter chain. @SpringBootTest loads the full context to prove real wiring, transactions, persistence, and security enforcement.

Does Spring Boot 4 change how MockMvc tests are configured?▼

Yes. On Spring Boot 4, @SpringBootTest no longer contributes MockMvc automatically, so @AutoConfigureMockMvc is mandatory, and the MVC slice arrives through spring-boot-starter-webmvc-test. RestTestClient replaces TestRestTemplate for random-port tests.

Should I use @WithMockUser in Spring Security integration tests?▼

No. @WithMockUser, security request post-processors, and mocked JwtDecoders bypass the filter chain the test exists to prove. Obtain a real token through the service's issuance path or an isolated test provider and send it in the Authorization header.

Why do my Spring Boot integration tests not run in the build?▼

The *IntegrationTest suffix requires explicit lifecycle configuration. Maven Surefire's default pattern can run them twice in the wrong phase, while Gradle has no default integration task so an unregistered suite never executes. Configure Failsafe or a registered Gradle task and record the commands.

When should I use @DataJpaTest instead of a full integration test?▼

Use @DataJpaTest only when a custom query, mapping, converter, constraint, locking, or database-specific persistence rule needs direct proof against the real database. Inherited JpaRepository CRUD behavior is already covered by full application integration tests.