askill
spring-boot-testing

spring-boot-testingSafety 100Repository

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.

0 stars
1.2k downloads
Updated 2/5/2026

Package Files

Loading files...
SKILL.md

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@MockitoBeanRequired migration
@SpyBean@MockitoSpyBeanRequired migration
Implicit @AutoConfigureMockMvcExplicit annotation requiredAdd to @SpringBootTest

Test Annotation Selection

Test TypeAnnotationUse When
Controller@WebMvcTestTesting request/response, validation
Repository@DataJpaTestTesting queries, entity mapping
JSON@JsonTestTesting serialization/deserialization
REST Client@RestClientTestTesting external API clients
Full Integration@SpringBootTestEnd-to-end, with real dependencies
Module@ApplicationModuleTestTesting bounded context in isolation

Core Workflow

  1. Choose test slice → Minimal context for fast tests
  2. Mock dependencies@MockitoBean for external services
  3. Use Testcontainers@ServiceConnection for databases
  4. Assert thoroughly → Use AssertJ, MockMvcTester, WebTestClient
  5. Test security@WithMockUser, JWT mocking

Quick Patterns

See EXAMPLES.md for complete working examples including:

  • @WebMvcTest with MockMvcTester and @MockitoBean (Java + Kotlin)
  • @DataJpaTest with TestEntityManager for lazy loading verification
  • Testcontainers with @ServiceConnection for PostgreSQL/Redis
  • Security Testing with @WithMockUser for role-based access
  • Modulith Event Testing with Scenario API

Detailed References

Anti-Pattern Checklist

Anti-PatternFix
Using @MockBean in Boot 4Replace with @MockitoBean
@SpringBootTest for unit testsUse appropriate slice annotation
Missing entityManager.clear()Add to verify lazy loading
High-cardinality test dataUse minimal, focused fixtures
Shared mutable test stateUse @DirtiesContext or fresh containers
No security testsAdd @WithMockUser tests for endpoints

Critical Reminders

  1. @MockitoBean is mandatory@MockBean removed in Boot 4
  2. Slice tests are fast — Use them for focused testing
  3. Clear EntityManager — Required to test lazy loading behavior
  4. @ServiceConnection simplifies Testcontainers — No more @DynamicPropertySource
  5. Test security explicitly — Don't rely on disabled security

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

95/100Analyzed 2/13/2026

A comprehensive and well-structured guide for Spring Boot 4 testing. It effectively covers critical migrations (e.g., @MockitoBean), annotation selection strategies, workflows, and anti-patterns. The use of tables and checklists makes the high-density technical information easy to digest.

100
95
95
90
90

Metadata

Licenseunknown
Version-
Updated2/5/2026
Publishermajiayu000

Tags

apidatabasegithub-actionssecuritytesting