
Spring Boot Test Patterns
Pick the right Spring Boot test slice—unit, @DataJpaTest, @WebMvcTest, or Testcontainers—and hit the documented speed targets while writing suites.
Overview
Spring-boot-test-patterns is an agent skill for the Ship phase that guides Spring Boot unit, slice, integration, and Testcontainers testing with JUnit 5 and Mockito.
Install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill spring-boot-test-patternsWhat is this skill?
- Quick-reference matrix: unit (<50ms), repository/controller slices (<100ms), full integration (<500ms)
- JUnit 5 + Mockito unit tests with @ExtendWith(MockitoExtension.class) without Spring context
- Slice tests: @DataJpaTest, @WebMvcTest, @WebFluxTest for focused layers
- Integration tests with Testcontainers and @ServiceConnection for Spring Boot 3.5+ container wiring
- allowed-tools: Read, Write, Edit, Bash, Glob, Grep for implementing and running test suites
- Quick reference targets unit tests under 50ms, slice tests under 100ms, and full @SpringBootTest integration under 500ms
Adoption & trust: 1.5k installs on skills.sh; 271 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You are writing Spring Boot tests but do not know which annotation stack to use, so suites are either too slow or too shallow to catch regressions.
Who is it for?
Indie developers on Spring Boot 3.x codebases who need a consistent testing ladder from fast unit tests to Testcontainers integration.
Skip if: Non-JVM stacks, front-end-only projects, or teams that only want one-line smoke tests without layering discipline.
When should I use this skill?
Writing tests, @Test methods, @MockBean mocks, or implementing test suites for Spring Boot applications.
What do I get? / Deliverables
You implement tests matched to layer and speed goals—mocked units, slice tests, or container-backed integration—using the documented annotation and timing patterns.
- Layered test classes using documented annotations and mocking patterns
- Integration tests with container-backed database or service connections where appropriate
Recommended Skills
Journey fit
How it compares
Skill playbook for Spring test annotations and containers—not a hosted CI service or a one-click coverage generator.
Common Questions / FAQ
Who is spring-boot-test-patterns for?
Solo and small-team builders maintaining Spring Boot APIs or services who want JUnit 5, Mockito, and Testcontainers patterns in one place.
When should I use spring-boot-test-patterns?
Use it in Ship when writing @Test methods, @MockBean mocks, REST controller tests, repository tests, or full integration suites with Testcontainers.
Is spring-boot-test-patterns safe to install?
The skill can run Bash and edit files per allowed-tools; review the Security Audits panel on this Prism page and run tests in isolated CI before trusting container or shell steps on production repos.
SKILL.md
READMESKILL.md - Spring Boot Test Patterns
# Spring Boot Testing Patterns ## Overview Comprehensive guidance for writing robust test suites for Spring Boot applications using JUnit 5, Mockito, Testcontainers, and performance-optimized slice testing patterns. ## When to Use - Writing unit tests for services or repositories with mocked dependencies - Implementing integration tests with real databases via Testcontainers - Testing REST APIs with `@WebMvcTest` or MockMvc - Configuring `@ServiceConnection` for container management in Spring Boot 3.5+ ## Quick Reference | Test Type | Annotation | Target Time | Use Case | |-----------|------------|-------------|----------| | **Unit Tests** | `@ExtendWith(MockitoExtension.class)` | < 50ms | Business logic without Spring context | | **Repository Tests** | `@DataJpaTest` | < 100ms | Database operations with minimal context | | **Controller Tests** | `@WebMvcTest` / `@WebFluxTest` | < 100ms | REST API layer testing | | **Integration Tests** | `@SpringBootTest` | < 500ms | Full application context with containers | | **Testcontainers** | `@ServiceConnection` / `@Testcontainers` | Varies | Real database/message broker containers | ## Core Concepts ### Test Architecture Philosophy 1. **Unit Tests** — Fast, isolated tests without Spring context (< 50ms) 2. **Slice Tests** — Minimal Spring context for specific layers (< 100ms) 3. **Integration Tests** — Full Spring context with real dependencies (< 500ms) ### Key Annotations **Spring Boot Test:** - `@SpringBootTest` — Full application context (use sparingly) - `@DataJpaTest` — JPA components only (repositories, entities) - `@WebMvcTest` — MVC layer only (controllers, `@ControllerAdvice`) - `@WebFluxTest` — WebFlux layer only (reactive controllers) - `@JsonTest` — JSON serialization components only **Testcontainers:** - `@ServiceConnection` — Wire Testcontainer to Spring Boot (3.5+) - `@DynamicPropertySource` — Register dynamic properties at runtime - `@Testcontainers` — Enable Testcontainers lifecycle management ## Instructions ### 1. Unit Testing Pattern Test business logic with mocked dependencies: ```java @ExtendWith(MockitoExtension.class) class UserServiceTest { @Mock private UserRepository userRepository; @InjectMocks private UserService userService; @Test void shouldFindUserByIdWhenExists() { when(userRepository.findById(1L)).thenReturn(Optional.of(user)); Optional<User> result = userService.findById(1L); assertThat(result).isPresent(); verify(userRepository).findById(1L); } } ``` See [unit-testing.md](references/unit-testing.md) for advanced patterns. ### 2. Slice Testing Pattern Use focused test slices for specific layers: ```java @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @TestContainerConfig class UserRepositoryIntegrationTest { @Autowired private UserRepository userRepository; @Test void shouldSaveAndRetrieveUser() { User saved = userRepository.save(user); assertThat(userRepository.findByEmail("test@example.com")).isPresent(); } } ``` See [slice-testing.md](references/slice-testing.md) for all slice patterns. ### 3. REST API Testing Pattern Test controllers with MockMvc: ```java @WebMvcTest(UserController.class) class UserControllerTest { @Autowired private MockMvc mockMvc; @MockBean private UserService userService; @Test void shouldGetUserById() throws Exception { mockMvc.perform(get("/api/users/1")) .andExpect(status().isOk()) .andExpec