Spring boot testing
Skill joaquimscosta/arkhe-claude-plugins/plugins/spring-boot/skills/spring-boot-testing
Spring Boot 4 testing strategies and patterns. Use when writing unit tests, slice tests (@WebMvcTest, @DataJpaTest), integration tests, Testcontainers with @ServiceConnection, security testing (@WithMockUser, JWT), or Modulith event testing with Scenario API. Covers the critical @MockitoBean migration from @MockBean.From its SKILL.md
npx -y skills add joaquimscosta/arkhe-claude-plugins --skill spring-boot-testingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 21 stars21 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
4.6 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
Spring Boot 4 Testing
Comprehensive testing patterns including slice tests, Testcontainers, security testing, and Modulith Scenario API.
Critical Breaking Change
| Old (Boot 3.x) | New (Boot 4.x) | Notes |
|---|---|---|
@MockBean | @MockitoBean | Required migration |
@SpyBean | @MockitoSpyBean | Required migration |
MockMvc (procedural) | MockMvcTester (fluent) | New AssertJ-style API |
Implicit @AutoConfigureMockMvc | Explicit annotation required | Add to @SpringBootTest |
MockMvcTester (Spring Boot 4)
New fluent, AssertJ-style API for controller testing:
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvcTester mvc; // NEW: Fluent API
@Test
void getUser_returnsUser() {
mvc.get().uri("/users/{id}", 1)
.exchange()
.assertThat()
.hasStatusOk()
.bodyJson()
.extractingPath("$.name")
.isEqualTo("John");
}
}
Key Benefits: Fluent assertions, better error messages, AssertJ integration.
Test Annotation Selection
| Test Type | Annotation | Use When |
|---|---|---|
| Controller | @WebMvcTest | Testing request/response, validation |
| Repository | @DataJpaTest | Testing queries, entity mapping |
| JSON | @JsonTest | Testing serialization/deserialization |
| REST Client | @RestClientTest | Testing external API clients |
| Full Integration | @SpringBootTest | End-to-end, with real dependencies |
| Module | @ApplicationModuleTest | Testing bounded context in isolation |
Core Workflow
- Choose test slice → Minimal context for fast tests
- Mock dependencies →
@MockitoBeanfor external services - Use Testcontainers →
@ServiceConnectionfor databases - Assert thoroughly → Use AssertJ,
MockMvcTester(new),RestTestClient(new), WebTestClient - Test security →
@WithMockUser, JWT mocking
Quick Patterns
See EXAMPLES.md for complete working examples including:
- @WebMvcTest with
MockMvcTesterand@MockitoBean(Java + Kotlin) - @DataJpaTest with
TestEntityManagerfor lazy loading verification - Testcontainers with
@ServiceConnectionfor PostgreSQL/Redis - Security Testing with
@WithMockUserfor role-based access - Modulith Event Testing with
ScenarioAPI
Detailed References
- Examples: See EXAMPLES.md for complete working code examples
- Troubleshooting: See TROUBLESHOOTING.md for common issues and Boot 4 migration
- Slice Tests: See references/SLICE-TESTS.md for @WebMvcTest, @DataJpaTest, @JsonTest patterns
- Testcontainers: See references/TESTCONTAINERS.md for @ServiceConnection, container reuse
- Security Testing: See references/SECURITY-TESTING.md for @WithMockUser, JWT mocking
- Modulith Testing: See references/MODULITH-TESTING.md for Scenario API, event verification
Anti-Pattern Checklist
| Anti-Pattern | Fix |
|---|---|
Using @MockBean in Boot 4 | Replace with @MockitoBean |
@SpringBootTest for unit tests | Use appropriate slice annotation |
Missing entityManager.clear() | Add to verify lazy loading |
| High-cardinality test data | Use minimal, focused fixtures |
| Shared mutable test state | Use @DirtiesContext or fresh containers |
| No security tests | Add @WithMockUser tests for endpoints |
Related Skills
| Need | Skill |
|---|---|
| Security configuration | spring-boot-security |
| Module boundaries | spring-boot-modulith |
| Data layer patterns | spring-boot-data-ddd |
| Controller patterns | spring-boot-web-api |
Critical Reminders
- @MockitoBean is mandatory —
@MockBeanremoved in Boot 4 - Slice tests are fast — Use them for focused testing
- Clear EntityManager — Required to test lazy loading behavior
- @ServiceConnection simplifies Testcontainers — No more
@DynamicPropertySource - Test security explicitly — Don't rely on disabled security
What ships with it: 6 files
57.8 KB alongside SKILL.md
references/
- MODULITH-TESTING.md12.3 KB
- SECURITY-TESTING.md12.0 KB
- SLICE-TESTS.md11.8 KB
- TESTCONTAINERS.md10.2 KB
- EXAMPLES.md5.5 KB
- TROUBLESHOOTING.md5.9 KB