unit-test-caching

Installation
Summary

Testing Spring Cache annotations without external infrastructure using in-memory cache managers.

  • Covers testing @Cacheable, @CacheEvict, and @CachePut annotations with patterns for verifying cache hits, misses, and invalidation
  • Uses ConcurrentMapCacheManager for fast, isolated unit tests instead of Redis or other external caches
  • Includes verification strategies via mock call counts, conditional caching with unless and condition parameters, and custom cache key generation with SpEL expressions
  • Provides setup examples for Maven and Gradle, plus complete test patterns for cache behavior, eviction, updates, and edge cases like null result handling
SKILL.md

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

Instructions

  1. Configure in-memory CacheManager: Use ConcurrentMapCacheManager for tests
  2. Set up test fixtures: Mock repository and create service instance in @BeforeEach
Related skills

More from giuseppe-trisciuoglio/developer-kit

Installs
868
GitHub Stars
246
First Seen
Feb 3, 2026