Class MockProxy

java.lang.Object
cloud.opencode.base.test.mock.MockProxy

public final class MockProxy extends Object
Mock Proxy - Factory for creating mock proxy instances Mock代理 - 创建Mock代理实例的工厂

Creates dynamic proxy instances for interface mocking in tests.

为测试中的接口Mock创建动态代理实例。

Features | 主要功能:

  • JDK dynamic proxy-based mock creation - 基于JDK动态代理的Mock创建
  • Multiple interface support - 多接口支持
  • Invocation recording and verification - 调用记录和验证
  • Mock detection and reset - Mock检测和重置

Usage Examples | 使用示例:

// Create a mock
UserService mock = MockProxy.create(UserService.class);

// Get the handler to configure behavior
MockInvocationHandler handler = MockProxy.getHandler(mock);
handler.when("findById", 1L).thenReturn(new User("John"));

// Use the mock
User user = mock.findById(1L);

Security | 安全性:

  • Thread-safe: Yes (stateless factory, thread-safe handler) - 线程安全: 是(无状态工厂,线程安全处理器)
  • Null-safe: Yes (validates non-null inputs) - 空值安全: 是(验证非空输入)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static <T> T create(Class<T> interfaceType)
      Creates a mock proxy for the given interface. 为给定接口创建Mock代理。
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceType - the interface type | 接口类型
      Returns:
      the mock instance | Mock实例
    • create

      public static Object create(Class<?>... interfaces)
      Creates a mock proxy for multiple interfaces. 为多个接口创建Mock代理。
      Parameters:
      interfaces - the interfaces | 接口
      Returns:
      the mock instance | Mock实例
    • getHandler

      public static MockInvocationHandler getHandler(Object mock)
      Gets the invocation handler for a mock. 获取Mock的调用处理器。
      Parameters:
      mock - the mock instance | Mock实例
      Returns:
      the handler | 处理器
    • isMock

      public static boolean isMock(Object object)
      Checks if object is a mock. 检查对象是否是Mock。
      Parameters:
      object - the object | 对象
      Returns:
      true if mock | 如果是Mock返回true
    • getInvocations

      public static List<Invocation> getInvocations(Object mock)
      Gets all invocations recorded on a mock. 获取Mock上记录的所有调用。
      Parameters:
      mock - the mock instance | Mock实例
      Returns:
      the invocations | 调用列表
    • clearInvocations

      public static void clearInvocations(Object mock)
      Clears all invocations on a mock. 清除Mock上的所有调用。
      Parameters:
      mock - the mock instance | Mock实例
    • reset

      public static void reset(Object mock)
      Resets a mock (clears invocations and stubbing). 重置Mock(清除调用和存根)。
      Parameters:
      mock - the mock instance | Mock实例
    • verify

      public static MockProxy.MockVerification verify(Object mock)
      Verifies that a method was called on the mock. 验证Mock上的方法被调用。
      Parameters:
      mock - the mock instance | Mock实例
      Returns:
      the verification | 验证
    • verify

      public static MockProxy.MockVerification verify(Object mock, int times)
      Verifies that a method was called exactly n times. 验证方法恰好被调用n次。
      Parameters:
      mock - the mock instance | Mock实例
      times - the expected times | 期望次数
      Returns:
      the verification | 验证