Class MockBuilder<T>

java.lang.Object
cloud.opencode.base.test.mock.MockBuilder<T>
Type Parameters:
T - the interface type | 接口类型

Performance | 性能特性:

  • Time complexity: O(1) to build; O(1) per method invocation on the mock - 时间复杂度: O(1) 构建;O(1) 每次方法调用
  • Space complexity: O(m) where m is the number of registered method handlers - 空间复杂度: O(m),m 为注册的方法处理器数量

public class MockBuilder<T> extends Object
Mock Builder 模拟构建器

Simple mock builder for interfaces.

接口的简单模拟构建器。

Features | 主要功能:

  • Core functionality - 核心功能

Usage Examples | 使用示例:

// Use type
type instance = ...;

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static <T> MockBuilder<T> of(Class<T> interfaceType)
      Create builder for interface 为接口创建构建器
      Type Parameters:
      T - the type | 类型
      Parameters:
      interfaceType - the interface type | 接口类型
      Returns:
      the builder | 构建器
    • when

      public MockBuilder<T> when(String methodName, Object returnValue)
      When method called, return value 当方法调用时,返回值
      Parameters:
      methodName - the method name | 方法名称
      returnValue - the return value | 返回值
      Returns:
      this builder | 此构建器
    • when

      public MockBuilder<T> when(String methodName, Function<Object[],Object> handler)
      When method called, execute function 当方法调用时,执行函数
      Parameters:
      methodName - the method name | 方法名称
      handler - the handler function | 处理函数
      Returns:
      this builder | 此构建器
    • defaultReturn

      public MockBuilder<T> defaultReturn(Object defaultValue)
      Set default return value 设置默认返回值
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      this builder | 此构建器
    • defaultHandler

      public MockBuilder<T> defaultHandler(Function<Method,Object> handler)
      Set default handler 设置默认处理器
      Parameters:
      handler - the handler | 处理器
      Returns:
      this builder | 此构建器
    • build

      public T build()
      Build the mock 构建模拟对象
      Returns:
      the mock instance | 模拟实例
    • mock

      public static <T> T mock(Class<T> interfaceType)
      Quick create mock with default values 快速创建带默认值的模拟
      Type Parameters:
      T - the type | 类型
      Parameters:
      interfaceType - the interface type | 接口类型
      Returns:
      the mock | 模拟对象