- Home
- Skills
- Giuseppe Trisciuoglio
- Developer Kit
- Spring Boot Test Patterns
spring-boot-test-patterns_skill
- Python
99
GitHub Stars
1
Bundled Files
3 weeks ago
Catalog Refreshed
2 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstart where the catalogue uses aiagentskills.
npx veilstart add skill giuseppe-trisciuoglio/developer-kit --skill spring-boot-test-patterns- SKILL.md16.6 KB
Overview
This skill provides practical, battle-tested testing patterns for Spring Boot applications. It covers unit, slice, integration, container-based, and reactive testing using JUnit 5, Mockito, Testcontainers, and Spring Boot test annotations. It also includes performance tips for fast feedback in local and CI environments.
How this skill works
The skill organizes testing into layers: isolated unit tests with Mockito, focused slice tests (@DataJpaTest, @WebMvcTest, @WebFluxTest), and full integration tests (@SpringBootTest) wired to real containers using @ServiceConnection. It demonstrates container setup with Testcontainers, dynamic property registration, and strategies for context caching and container reuse to optimize execution time. Example code snippets show patterns for controllers, repositories, services, and reactive endpoints.
When to use it
- When writing fast, isolated unit tests for services and utilities.
- When validating persistence logic with @DataJpaTest and a real database via Testcontainers.
- When testing controllers or web layers using @WebMvcTest, MockMvc, or WebTestClient.
- When running end-to-end integration tests with @SpringBootTest and containerized databases.
- When optimizing CI pipelines for speed using context caching and container reuse.
Best practices
- Pick the narrowest test slice that verifies the behavior to keep tests fast and deterministic.
- Use Mockito and @MockBean for external collaborators in unit and slice tests to avoid starting the full context.
- Prefer @ServiceConnection (Spring Boot 3.5+) to wire Testcontainers into the application context cleanly.
- Group tests by configuration to maximize Spring context caching and reduce startup overhead.
- Reuse Testcontainers at the JVM level in CI and local runs to minimize container startup time.
Example use cases
- Unit test a service method with Mockito and assert business logic in <50ms.
- Use @DataJpaTest plus a PostgreSQL Testcontainer to verify repository queries against a real DB.
- Test a REST controller with @WebMvcTest and MockMvc to validate JSON responses and status codes.
- Run a full integration test with @SpringBootTest and @ServiceConnection-backed Postgres to verify transaction flows.
- Create reactive endpoint tests with WebTestClient on a random port SpringBootTest.
FAQ
No. Testcontainers are recommended for realistic integration tests and CI portability, but you can use embedded databases or shared CI services if appropriate.
When should I use @SpringBootTest vs slice tests?
Use slice tests for focused layer verification (faster). Use @SpringBootTest only when you need the full application context or to validate cross-cutting behavior.