Class OpenMock

java.lang.Object
cloud.opencode.base.test.OpenMock

public final class OpenMock extends Object
Mock Entry Class - Provides interface mocking capabilities Mock入口类 - 提供接口模拟能力

Zero-dependency mock library for creating interface mocks without external bytecode manipulation libraries.

零依赖模拟库,无需外部字节码操作库即可创建接口模拟。

Limitations | 限制:

  • Only supports interface mocking (not classes) - 仅支持接口模拟(非类)
  • Uses JDK Proxy - 使用JDK代理

Features | 主要功能:

  • Create mock interfaces - 创建模拟接口
  • Stub method returns - 桩方法返回值
  • Verify invocations - 验证调用
  • Thread-safe - 线程安全

Usage Examples | 使用示例:

// Simple mock
UserService mock = OpenMock.mock(UserService.class);

// With stubbing
UserService mock = OpenMock.when(UserService.class)
    .thenReturn("getName", "John")
    .thenReturn("getAge", 25)
    .build();

// Verify invocations
OpenMock.verify(mock).wasInvoked("getName");

Security | 安全性:

  • Thread-safe: Yes (synchronized handler map, ConcurrentHashMap stubs) - 线程安全: 是(同步处理器映射,ConcurrentHashMap存根)
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Invocation record 调用记录
    static class 
    Mock Builder for fluent mock configuration 用于流畅mock配置的Mock构建器
    static class 
    Verification wrapper for asserting mock invocations 用于断言mock调用的验证包装器
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    mock(Class<T> type)
    Creates a mock of the given interface type 创建给定接口类型的模拟
    static void
    reset(Object mock)
    Resets all recorded invocations for the mock 重置模拟的所有记录调用
    static <T> OpenMock.Verification<T>
    verify(T mock)
    Gets verification wrapper for the mock 获取模拟的验证包装器
    static <T> OpenMock.MockBuilder<T>
    when(Class<T> type)
    Creates a mock builder for fluent configuration 创建用于流畅配置的模拟构建器

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • mock

      public static <T> T mock(Class<T> type)
      Creates a mock of the given interface type 创建给定接口类型的模拟
      Type Parameters:
      T - the type | 类型
      Parameters:
      type - the interface type to mock | 要模拟的接口类型
      Returns:
      the mock instance | 模拟实例
    • when

      public static <T> OpenMock.MockBuilder<T> when(Class<T> type)
      Creates a mock builder for fluent configuration 创建用于流畅配置的模拟构建器
      Type Parameters:
      T - the type | 类型
      Parameters:
      type - the interface type to mock | 要模拟的接口类型
      Returns:
      the mock builder | 模拟构建器
    • verify

      public static <T> OpenMock.Verification<T> verify(T mock)
      Gets verification wrapper for the mock 获取模拟的验证包装器
      Type Parameters:
      T - the type | 类型
      Parameters:
      mock - the mock instance | 模拟实例
      Returns:
      the verification wrapper | 验证包装器
    • reset

      public static void reset(Object mock)
      Resets all recorded invocations for the mock 重置模拟的所有记录调用
      Parameters:
      mock - the mock instance | 模拟实例