github/awesome-copilot / skills/spring-boot-testing
spring-boot-testing
UnverifiedExpert Spring Boot testing specialist that selects the best testing techniques for your situation: test slices, Testcontainers with @ServiceConnection, Failsafe integration tests, MockitoBean, JUnit and AssertJ. Written for Spring Boot 4 with notes for Boot 3.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.3 KB |
| references/assertj-basics.md | markdown | 4.3 KB |
| references/assertj-collections.md | markdown | 4.9 KB |
| references/context-caching.md | markdown | 2.4 KB |
| references/datajpatest.md | markdown | 5.0 KB |
| references/failsafe-integration-tests.md | markdown | 2.5 KB |
| references/instancio.md | markdown | 4.9 KB |
| references/mockitobean.md | markdown | 4.5 KB |
| references/mockmvc-classic.md | markdown | 5.8 KB |
| references/mockmvc-tester.md | markdown | 7.5 KB |
| references/restclienttest.md | markdown | 4.6 KB |
| references/resttestclient.md | markdown | 4.9 KB |
| references/sb4-migration.md | markdown | 3.3 KB |
| references/test-slices-overview.md | markdown | 5.5 KB |
| references/testcontainers-jdbc.md | markdown | 7.7 KB |
| references/webmvctest.md | markdown | 3.6 KB |
references/context-caching.md
View on GitHub# Context Caching
Optimize Spring Boot test suite performance through context caching.
## How Context Caching Works
Spring's TestContext Framework caches application contexts based on their configuration "key". Tests with identical configurations reuse the same context.
### What Affects the Cache Key
- @ContextConfiguration
- @TestPropertySource
- @ActiveProfiles
- @WebAppConfiguration
- @MockitoBean definitions
- @TestConfiguration imports
## Cache Key Examples
### Same Key (Context Reused)
```java
@WebMvcTest(OrderController.class)
class OrderControllerTest1 {
@MockitoBean private OrderService orderService;
}
@WebMvcTest(OrderController.class)
class OrderControllerTest2 {
@MockitoBean private OrderService orderService;
}
// Same context reused
```
### Different Key (New Context)
```java
@WebMvcTest(OrderController.class)
@ActiveProfiles("test")
class OrderControllerTest1 { }
@WebMvcTest(OrderController.class)
@ActiveProfiles("integration")
class OrderControllerTest2 { }
// Different contexts loaded
```
## Viewing Cache Statistics
### Spring Boot Actuator
```yaml
management:
endpoints:
web:
exposure:
include: metrics
```
Access: `GET /actuator/metrics/spring.test.context.cache`
### Debug Logging
```properties
logging.level.org.springframework.test.context.cache=DEBUG
```
## Optimizing Cache Hit Rate
### Group Tests by Configuration
```
tests/
unit/ # No context
web/ # @WebMvcTest
repository/ # @DataJpaTest
integration/ # @SpringBootTest
```
### Minimize @TestPropertySource Variations
**Bad (multiple contexts):**
```java
@TestPropertySource(properties = "app.feature-x=true")
class FeatureXTest { }
@TestPropertySource(properties = "app.feature-y=true")
class FeatureYTest { }
```
**Better (grouped):**
```java
@TestPropertySource(properties = {"app.feature-x=true", "app.feature-y=true"})
class FeaturesTest { }
```
### Use @DirtiesContext Sparingly
Only when context state truly changes:
```java
@Test
@DirtiesContext // Forces context rebuild after test
void testThatModifiesBeanDefinitions() { }
```
## Best Practices
1. **Group by configuration** - Keep tests with same config together
2. **Limit property variations** - Use profiles over individual properties
3. **Avoid @DirtiesContext** - Prefer test data cleanup
4. **Use narrow slices** - @WebMvcTest vs @SpringBootTest
5. **Monitor cache hits** - Enable debug logging occasionally
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |