skillfed

unit-test-caching

This skill equips you with reusable test patterns for Spring caching annotations without requiring full application context. It covers cache hit/miss behavior, invalidation logic, SpEL key generation, and conditional caching scenarios using in-memory CacheManager.

unit-test-caching generates test patterns for Spring Cache annotations including @Cacheable, @CacheEvict, and @CachePut verification.

AI-generated summary based on this skill's SKILL.md

311 37 MIT updated by giuseppe-trisciuoglio

Install

giuseppe-trisciuoglio/developer-kit/unit-test-caching · repository language: Python

git clone https://github.com/giuseppe-trisciuoglio/developer-kit
cp -r developer-kit/plugins/developer-kit-java/skills/unit-test-caching ~/.claude/skills/unit-test-caching
npx skillfed install giuseppe-trisciuoglio/developer-kit/unit-test-caching

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How do I unit test Spring @Cacheable annotation?

unit-test-caching teaches you to isolate @Cacheable behavior by injecting a ConcurrentMapCacheManager into your test, then verifying cache hits by checking that your repository method is called fewer times than the number of invocations. Use Mockito's times() matcher to assert call counts and confirm the cache prevented redundant database queries.

How can unit-test-caching help verify cache hit/miss behavior?

unit-test-caching provides patterns to verify cache hit/miss by counting repository invocations with Mockito.verify(). For cache hits, the repository is called once but the method is invoked multiple times; for misses, call counts match invocation counts. This skill shows how to assert these patterns without requiring Redis or external cache infrastructure.

What does unit-test-caching teach about testing @CacheEvict invalidation?

unit-test-caching covers @CacheEvict testing by demonstrating how to verify that cache entries are removed after eviction methods execute. You'll learn to check that subsequent calls to the cached method trigger repository calls again, confirming the cache was actually cleared rather than returning stale data.

Can unit-test-caching help test cache key generation from SpEL expressions?

Yes. unit-test-caching includes patterns for testing SpEL-based cache keys and compound keys by verifying that different parameter combinations produce distinct cache entries. You'll learn to mock CacheManager and inspect cache contents to confirm key generation logic works correctly without needing complex integration tests.

How does unit-test-caching address conditional caching with unless/condition?

unit-test-caching teaches you to mock cache managers and test conditional caching by verifying that @Cacheable with unless or condition attributes correctly skips caching when predicates are false. You'll use Mockito to confirm the cache.get() method is never called for excluded conditions, validating your conditional logic in isolation.

What patterns does unit-test-caching provide for null result handling?

unit-test-caching covers null result handling by showing how to test @Cacheable and @CachePut behavior when methods return null. You'll learn whether your cache stores null values and how to validate cache eviction strategies that may treat nulls differently, ensuring your application handles edge cases consistently.

SKILL.md

rendered from the published skill — quoted content, verbatim

Unit Testing Spring Caching

Overview

This skill provides patterns for unit testing Spring caching annotations (@Cacheable, @CacheEvict, @CachePut) without full Spring context. It covers cache hits/misses, invalidation, key generation, and conditional caching using in-memory ConcurrentMapCacheManager.

When to Use

  • Writing unit tests for @Cacheable method behavior
  • Verifying @CacheEvict cache invalidation works correctly
  • Testing @CachePut cache updates
  • Validating cache key generation from SpEL expressions
  • Testing conditional caching with unless/condition parameters
  • Mocking cache managers in fast unit tests without Redis

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
plugins/developer-kit-java/skills/unit-test-caching/SKILL.md

Related skills

Tags

cache-behavior-verification mock-cache-manager spel-key-generation cache-invalidation-testing conditional-caching-logic in-memory-cache-testing cache-hit-miss-patterns spring-proxy-testing cache-eviction-validation