Class CacheTestSupport
java.lang.Object
cloud.opencode.base.cache.testing.CacheTestSupport
Cache Test Support - Testing utilities for cache unit tests
缓存测试支持 - 缓存单元测试工具
Provides mock cache implementation and test utilities for unit testing cache-dependent code without real cache infrastructure.
提供模拟缓存实现和测试工具,用于对依赖缓存的代码进行单元测试, 无需真实的缓存基础设施。
Features | 主要功能:
- MockCache - Simple in-memory mock | 简单内存模拟
- TestClock - Controllable time for expiration testing | 可控时间用于过期测试
- RecordingCache - Records all operations | 记录所有操作
- Assertion helpers - 断言辅助方法
Usage Examples | 使用示例:
// Create mock cache - 创建模拟缓存
MockCache<String, User> cache = CacheTestSupport.mockCache();
cache.put("user:1", user);
assertThat(cache.get("user:1")).isEqualTo(user);
// Test expiration with controllable clock - 使用可控时钟测试过期
TestClock clock = CacheTestSupport.testClock();
MockCache<String, String> cache = CacheTestSupport.mockCache(clock);
cache.putWithTtl("key", "value", Duration.ofMinutes(5));
clock.advance(Duration.ofMinutes(6));
assertThat(cache.get("key")).isNull(); // Expired
// Record operations - 记录操作
RecordingCache<String, User> recording = CacheTestSupport.recordingCache();
recording.put("user:1", user);
assertThat(recording.operations()).contains(
new CacheOperation.Put("user:1", user)
);
Security | 安全性:
- Thread-safe: Yes (MockCache uses ConcurrentHashMap) - 线程安全: 是(MockCache 使用 ConcurrentHashMap)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSealed interface for cache operations 缓存操作密封接口static classSimple mock cache implementation for testing 用于测试的简单模拟缓存实现static classCache that records all operations for verification 记录所有操作以供验证的缓存static classControllable clock for testing time-dependent cache behavior 用于测试时间相关缓存行为的可控时钟 -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> CacheTestSupport.MockCache <K, V> Create a simple mock cache 创建简单模拟缓存static <K,V> CacheTestSupport.MockCache <K, V> Create mock cache with controllable clock 创建带可控时钟的模拟缓存static <K,V> CacheTestSupport.MockCache <K, V> Create mock cache with name 创建带名称的模拟缓存static <K,V> CacheTestSupport.RecordingCache <K, V> Create a recording cache that tracks operations 创建记录操作的缓存static CacheTestSupport.TestClockCreate a test clock 创建测试时钟static CacheTestSupport.TestClockCreate a test clock with initial time 创建带初始时间的测试时钟
-
Method Details
-
mockCache
Create a simple mock cache 创建简单模拟缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- mock cache | 模拟缓存
-
mockCache
Create mock cache with controllable clock 创建带可控时钟的模拟缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
clock- test clock | 测试时钟- Returns:
- mock cache | 模拟缓存
-
mockCache
Create mock cache with name 创建带名称的模拟缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称- Returns:
- mock cache | 模拟缓存
-
recordingCache
Create a recording cache that tracks operations 创建记录操作的缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- recording cache | 记录缓存
-
testClock
Create a test clock 创建测试时钟- Returns:
- test clock | 测试时钟
-
testClock
Create a test clock with initial time 创建带初始时间的测试时钟- Parameters:
initialTime- initial time | 初始时间- Returns:
- test clock | 测试时钟
-