mirror of
https://github.com/github/awesome-copilot.git
synced 2026-03-20 16:15:12 +00:00
feat: add spring-boot-testing skill for Spring Boot 4.0 (#1085)
- Introduced MockMvcTester for AssertJ-style assertions in Spring MVC testing. - Added @RestClientTest for testing REST clients with MockRestServiceServer. - Implemented RestTestClient as a modern alternative to TestRestTemplate. - Documented migration steps from Spring Boot 3.x to 4.0, including dependency and annotation changes. - Created an overview of test slices to guide testing strategies. - Included Testcontainers setup for JDBC testing with PostgreSQL and MySQL. - Enhanced @WebMvcTest documentation with examples for various HTTP methods and validation.
This commit is contained in:
115
skills/spring-boot-testing/references/context-caching.md
Normal file
115
skills/spring-boot-testing/references/context-caching.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# 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
|
||||
Reference in New Issue
Block a user