Class MockInvocationHandler

java.lang.Object
cloud.opencode.base.test.mock.MockInvocationHandler
All Implemented Interfaces:
InvocationHandler

public final class MockInvocationHandler extends Object implements InvocationHandler
Mock Invocation Handler - Handles method invocations on mock proxies Mock调用处理器 - 处理Mock代理上的方法调用

Records method invocations and returns stubbed values.

记录方法调用并返回存根值。

Features | 主要功能:

  • Invocation recording with timestamps - 带时间戳的调用记录
  • Method stubbing (return value, throw, answer) - 方法存根(返回值、抛异常、应答)
  • Invocation counting and filtering - 调用计数和过滤
  • Default value return for primitive types - 原始类型默认值返回

Usage Examples | 使用示例:

MockInvocationHandler handler = new MockInvocationHandler(UserService.class);

// Configure stubbing
handler.when("findById", 1L).thenReturn(new User("John"));
handler.when("save").thenAnswer(args -> args[0]);
handler.when("delete").thenThrow(new RuntimeException("Error"));

Security | 安全性:

  • Thread-safe: Yes (CopyOnWriteArrayList for invocations, ConcurrentHashMap for stubs) - 线程安全: 是(调用使用CopyOnWriteArrayList,存根使用ConcurrentHashMap)
  • Null-safe: Yes (validates mocked type) - 空值安全: 是(验证Mock类型)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • MockInvocationHandler

      public MockInvocationHandler(Class<?> mockedType)
      Creates handler for mocked type. 为Mock类型创建处理器。
      Parameters:
      mockedType - the mocked type | Mock类型
  • Method Details

    • invoke

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      Specified by:
      invoke in interface InvocationHandler
      Throws:
      Throwable
    • when

      public MockInvocationHandler.Stubbing when(String methodName, Object... args)
      Sets up stubbing for method. 为方法设置存根。
      Parameters:
      methodName - the method name | 方法名
      args - the expected arguments (or empty for any) | 期望参数(或空表示任意)
      Returns:
      the stubbing | 存根配置
    • getInvocations

      public List<Invocation> getInvocations()
      Gets all recorded invocations. 获取所有记录的调用。
      Returns:
      the invocations | 调用列表
    • getInvocations

      public List<Invocation> getInvocations(String methodName)
      Gets invocations for method. 获取方法的调用。
      Parameters:
      methodName - the method name | 方法名
      Returns:
      the invocations | 调用列表
    • countInvocations

      public int countInvocations(String methodName)
      Counts invocations for method. 统计方法的调用次数。
      Parameters:
      methodName - the method name | 方法名
      Returns:
      the count | 次数
    • countInvocations

      public int countInvocations(String methodName, Object... args)
      Counts invocations for method with specific args. 统计特定参数方法的调用次数。
      Parameters:
      methodName - the method name | 方法名
      args - the expected arguments | 期望参数
      Returns:
      the count | 次数
    • clearInvocations

      public void clearInvocations()
      Clears all invocations. 清除所有调用。
    • reset

      public void reset()
      Resets handler (clears invocations and stubs). 重置处理器(清除调用和存根)。
    • getMockedType

      public Class<?> getMockedType()
      Gets the mocked type. 获取Mock类型。
      Returns:
      the mocked type | Mock类型