- Home
- Skills
- Giuseppe Trisciuoglio
- Developer Kit
- Unit Test Caching
unit-test-caching_skill
- Python
99
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill giuseppe-trisciuoglio/developer-kit --skill unit-test-caching- SKILL.md12.8 KB
Overview
This skill provides concise, reusable patterns for unit testing Spring Cache annotations (@Cacheable, @CachePut, @CacheEvict) without booting a full Spring context. It focuses on fast, reliable tests using an in-memory CacheManager, mocks for dependencies, and explicit verification of cache hits, misses, and invalidation. Use it to validate cache configuration, key generation, and conditional caching behavior.
How this skill works
The skill shows how to replace external caches (Redis, etc.) with ConcurrentMapCacheManager in tests and how to configure @EnableCaching in a light test config. It uses Mockito to mock repositories and verifies repository invocation counts to assert cache hits and misses. Patterns cover @Cacheable caching, @CacheEvict invalidation and allEntries behavior, @CachePut updates, SpEL-based cache keys, and conditional caching with condition/unless.
When to use it
- Unit testing @Cacheable behavior and ensuring method execution is skipped on cache hit
- Validating @CacheEvict removes single keys or clears all entries
- Testing @CachePut updates cache entries after persistence operations
- Verifying custom cache key generation using SpEL expressions
- Testing conditional caching (condition and unless) and null-result handling
Best practices
- Use ConcurrentMapCacheManager or an in-memory cache for deterministic, fast tests
- Mock repositories and verify invocation counts (verify(times(n))) instead of relying on timing
- Reset or clear caches between tests (@BeforeEach or @DirtiesContext) to avoid cross-test leaks
- Test positive and negative paths: hits, misses, eviction, and conditional branches
- Ensure caching proxying is active in test config (@EnableCaching) and avoid direct self-invocation
Example use cases
- Assert getUserById is called once on repeated calls when @Cacheable is applied
- Confirm deleteProduct triggers @CacheEvict and subsequent reads hit repository again
- Validate updateOrder with @CachePut replaces cached value so subsequent reads return updated data
- Check InventoryService cache key '#productId + "-" + #warehouseId' generates distinct keys for different params
- Verify @Cacheable(unless = "#result == null") prevents caching of null repository results
FAQ
No. Use an in-memory CacheManager and a minimal @EnableCaching test configuration to exercise caching behavior without starting the full application.
How do I verify a cache hit in a unit test?
Mock the underlying repository and assert it was called only once while calling the cached method multiple times; use verify(times(1)).