Spring Boot Test Slices: @WebMvcTest and @DataJpaTest
That makes slices valuable only when the team knows exactly what each slice is supposed to prove.
Test slices exist to narrow proof targets
The core idea is simple:
- load less application context
- focus on one technical boundary
- get faster, clearer feedback
This helps prevent a common failure mode where every test becomes a full @SpringBootTest and the suite gets slow without becoming more informative.
@WebMvcTest is for HTTP contract behavior
@WebMvcTest is strongest when the goal is to verify:
- request mapping
- validation behavior
- status codes
- JSON response shape
- exception-to-response translation
- security behavior at the MVC layer
It is not meant to prove repository behavior or full service integration. Dependencies outside the slice should be provided deliberately through mocks or targeted test configuration.
@DataJpaTest is for persistence reality
@DataJpaTest is useful for proving:
- repository query correctness
- JPA mappings
- generated SQL behavior
- pagination and fetch behavior
- transaction interaction in the persistence layer
This is where teams should catch query mistakes, mapping surprises, and database-specific issues before they leak upward.
Other slices matter too
Depending on the codebase, focused slices like these can be valuable:
@JsonTestfor serialization and deserialization contracts@RestClientTestfor HTTP client boundaries- messaging or other specialized slices where supported
The underlying principle is always the same: keep the test focused on one boundary of proof.
Slices work best when application boundaries are already clear
A slice becomes awkward when the production design itself is overly tangled.
Common signs of trouble:
- controllers doing too much business logic
- services tightly coupled to unrelated infrastructure
- repositories carrying domain decisions
When boundaries are muddy in production code, slice tests often become mock-heavy and confusing. That is a design signal, not just a test problem.
@SpringBootTest still has an important role
Avoiding full-context tests entirely is not the goal. @SpringBootTest is useful when the team needs to prove:
- real cross-layer wiring
- configuration-sensitive behavior
- important integration paths
- a small number of high-value end-to-end application flows
The mistake is not using @SpringBootTest. The mistake is using it for every proof target.
Common anti-patterns
Watch for these patterns:
- using
@SpringBootTestfor everything - dragging unrelated dependencies into a slice test
- proving the same defect class redundantly in slice and full-context tests
- writing slice tests without a clear statement of what boundary is being verified
These patterns make the suite slower without making it clearer.
Review checklist
Before calling slice usage healthy, ask:
- What exact boundary is this test proving?
- Is this the cheapest realistic layer to prove it?
- Are dependencies outside the slice controlled intentionally?
- Would a full-context test add unique information here?
- Is the application design clear enough that the slice stays readable?
Closing judgment
Spring test slices are not just a speed hack. They are a responsibility-separation tool. The clearer the proof target, the healthier the overall test strategy becomes.
Continue Reading
Related posts
REST Assured API Testing Strategy Guide
A practical guide to testing Java-based APIs with REST Assured. Focuses on contract validation, authentication flows, test data, and integration-test boundaries rather than just request examples.
🧪 TestMock, Stub, and Spy Test Double Design Guide
A practical guide to choosing the right test double. Covers the difference between state verification and interaction verification, the risk of excessive mocking, and how to decide between mocks, stubs, spies, and fakes.
⚙️ BackendDesigning a Spring Boot REST API That Holds Up in Production
A production-focused guide to Spring Boot REST APIs. Learn how to keep controllers thin, contracts stable, transactions honest, and operational behavior predictable as the system grows.
⚙️ BackendA Practical Guide to Spring Boot JPA and Hibernate
This guide covers entity boundaries, relationship cost, N+1, DTO reads, transaction design, and operational pitfalls when using JPA in production.
Next Path