Class MockProxy
java.lang.Object
cloud.opencode.base.test.mock.MockProxy
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMock verification helper. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearInvocations(Object mock) Clears all invocations on a mock.static ObjectCreates a mock proxy for multiple interfaces.static <T> TCreates a mock proxy for the given interface.static MockInvocationHandlergetHandler(Object mock) Gets the invocation handler for a mock.static List<Invocation> getInvocations(Object mock) Gets all invocations recorded on a mock.static booleanChecks if object is a mock.static voidResets a mock (clears invocations and stubbing).static MockProxy.MockVerificationVerifies that a method was called on the mock.static MockProxy.MockVerificationVerifies that a method was called exactly n times.
-
Method Details
-
create
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
-
getHandler
Gets the invocation handler for a mock. 获取Mock的调用处理器。- Parameters:
mock- the mock instance | Mock实例- Returns:
- the handler | 处理器
-
isMock
Checks if object is a mock. 检查对象是否是Mock。- Parameters:
object- the object | 对象- Returns:
- true if mock | 如果是Mock返回true
-
getInvocations
Gets all invocations recorded on a mock. 获取Mock上记录的所有调用。- Parameters:
mock- the mock instance | Mock实例- Returns:
- the invocations | 调用列表
-
clearInvocations
Clears all invocations on a mock. 清除Mock上的所有调用。- Parameters:
mock- the mock instance | Mock实例
-
reset
Resets a mock (clears invocations and stubbing). 重置Mock(清除调用和存根)。- Parameters:
mock- the mock instance | Mock实例
-
verify
Verifies that a method was called on the mock. 验证Mock上的方法被调用。- Parameters:
mock- the mock instance | Mock实例- Returns:
- the verification | 验证
-
verify
Verifies that a method was called exactly n times. 验证方法恰好被调用n次。- Parameters:
mock- the mock instance | Mock实例times- the expected times | 期望次数- Returns:
- the verification | 验证
-