Provides patterns for unit testing Spring Cache annotations (@Cacheable, @CachePut, @CacheEvict). Generates test code that mocks cache managers, verifies cache hit/miss behavior, tests cache key generation with SpEL expressions, validates eviction strategies, and checks conditional caching scenarios. Triggers: caching tests, test Spring cache, mock cache, Spring Boot caching, cache hit/miss verification, @Cacheable testing.
This skill provides patterns for unit testing Spring caching annotations (@Cacheable, @CachePut, @CacheEvict) without a full Spring context. It solves the problem of confidently verifying cache behavior — hits, misses, invalidation, key generation, and conditional caching — in fast tests that use an in-memory ConcurrentMapCacheManager instead of Redis or another external cache.
The workflow configures an in-memory cache manager, sets up mocked repositories and service fixtures in @BeforeEach, and uses Mockito times(n) verifications to confirm how often the underlying repository is invoked. It demonstrates testing cache hits (method called twice, repository called once), cache misses, eviction (repository called again after @CacheEvict), SpEL-based compound key generation, and conditional caching with unless and condition parameters. Validation checkpoints help diagnose common failures: missing @EnableCaching, self-invocation bypassing the Spring proxy, and cache key mismatches. Worked examples cover a UserService with @Cacheable, a ProductService with @CacheEvict, and an OrderService with @CachePut.
It targets Java and Spring Boot developers who want fast, deterministic verification of caching logic without spinning up external cache infrastructure. It fits teams that rely heavily on declarative caching and need regression coverage for hit/miss and eviction semantics.
@Cacheable, @CachePut, and @CacheEvict, including conditional caching via unless and condition and SpEL key generation.
No. It uses an in-memory ConcurrentMapCacheManager so cache tests run fast without external infrastructure.
By mocking the repository and asserting invocation counts with Mockito times(n) — for example, verifying the repository is called only once when a value is cached.
Missing @EnableCaching, method self-invocation that bypasses the Spring proxy, and mismatched cache keys, each with a validation checkpoint.
spring-boot-starter-cache and spring-boot-starter-test, with Maven and Gradle snippets provided.
Quick Setup:
.claude/skills/Repository
giuseppe-trisciuoglio/developer-kit