Back to Skills

unit-test-caching

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.

315stars37forksUpdated 8/1/2026

Security Assessment

Safe(95/100)
Security Score95/100

About unit-test-caching

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.

FAQ

Which caching annotations does it cover?

@Cacheable, @CachePut, and @CacheEvict, including conditional caching via unless and condition and SpEL key generation.

Does it require Redis or an external cache?

No. It uses an in-memory ConcurrentMapCacheManager so cache tests run fast without external infrastructure.

How does it verify caching worked?

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.

What common pitfalls does it help diagnose?

Missing @EnableCaching, method self-invocation that bypasses the Spring proxy, and mismatched cache keys, each with a validation checkpoint.

What dependencies are needed?

spring-boot-starter-cache and spring-boot-starter-test, with Maven and Gradle snippets provided.

Install unit-test-caching

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill