AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

spring-boot-testing

Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.

Original source
github
License
MIT
Pinned revision
5269f7ea2d6c3a9bfda5ed878b6726cf509121fd

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read github--spring-boot-testing at revision 5269f7ea2d6c3a9bfda5ed878b6726cf509121fd, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 15

references/context-caching.md

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)

@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)

@WebMvcTest(OrderController.class)
@ActiveProfiles("test")
class OrderControllerTest1 { }

@WebMvcTest(OrderController.class)
@ActiveProfiles("integration")
class OrderControllerTest2 { }
// Different contexts loaded

Viewing Cache Statistics

Spring Boot Actuator

management:
  endpoints:
    web:
      exposure:
        include: metrics

Access: GET /actuator/metrics/spring.test.context.cache

Debug Logging

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):

@TestPropertySource(properties = "app.feature-x=true")
class FeatureXTest { }

@TestPropertySource(properties = "app.feature-y=true")
class FeatureYTest { }

Better (grouped):

@TestPropertySource(properties = {"app.feature-x=true", "app.feature-y=true"})
class FeaturesTest { }

Use @DirtiesContext Sparingly

Only when context state truly changes:

@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