Class CacheTestSupport

java.lang.Object
cloud.opencode.base.cache.testing.CacheTestSupport

public final class CacheTestSupport extends Object
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:
  • Method Details

    • mockCache

      public static <K,V> CacheTestSupport.MockCache<K,V> mockCache()
      Create a simple mock cache 创建简单模拟缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      mock cache | 模拟缓存
    • mockCache

      public static <K,V> CacheTestSupport.MockCache<K,V> mockCache(CacheTestSupport.TestClock clock)
      Create mock cache with controllable clock 创建带可控时钟的模拟缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      clock - test clock | 测试时钟
      Returns:
      mock cache | 模拟缓存
    • mockCache

      public static <K,V> CacheTestSupport.MockCache<K,V> mockCache(String name)
      Create mock cache with name 创建带名称的模拟缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      name - cache name | 缓存名称
      Returns:
      mock cache | 模拟缓存
    • recordingCache

      public static <K,V> CacheTestSupport.RecordingCache<K,V> recordingCache()
      Create a recording cache that tracks operations 创建记录操作的缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      recording cache | 记录缓存
    • testClock

      public static CacheTestSupport.TestClock testClock()
      Create a test clock 创建测试时钟
      Returns:
      test clock | 测试时钟
    • testClock

      public static CacheTestSupport.TestClock testClock(Instant initialTime)
      Create a test clock with initial time 创建带初始时间的测试时钟
      Parameters:
      initialTime - initial time | 初始时间
      Returns:
      test clock | 测试时钟