Class OpenMock
java.lang.Object
cloud.opencode.base.test.OpenMock
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 ClassesModifier and TypeClassDescriptionstatic final recordInvocation record 调用记录static classMock Builder for fluent mock configuration 用于流畅mock配置的Mock构建器static classVerification wrapper for asserting mock invocations 用于断言mock调用的验证包装器 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TCreates a mock of the given interface type 创建给定接口类型的模拟static voidResets 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> Creates a mock builder for fluent configuration 创建用于流畅配置的模拟构建器
-
Method Details
-
mock
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
Creates a mock builder for fluent configuration 创建用于流畅配置的模拟构建器- Type Parameters:
T- the type | 类型- Parameters:
type- the interface type to mock | 要模拟的接口类型- Returns:
- the mock builder | 模拟构建器
-
verify
Gets verification wrapper for the mock 获取模拟的验证包装器- Type Parameters:
T- the type | 类型- Parameters:
mock- the mock instance | 模拟实例- Returns:
- the verification wrapper | 验证包装器
-
reset
Resets all recorded invocations for the mock 重置模拟的所有记录调用- Parameters:
mock- the mock instance | 模拟实例
-