Class MockInvocationHandler
java.lang.Object
cloud.opencode.base.test.mock.MockInvocationHandler
- All Implemented Interfaces:
InvocationHandler
Mock Invocation Handler - Handles method invocations on mock proxies
Mock调用处理器 - 处理Mock代理上的方法调用
Records method invocations and returns stubbed values.
记录方法调用并返回存根值。
Features | 主要功能:
- Invocation recording with timestamps - 带时间戳的调用记录
- Method stubbing (return value, throw, answer) - 方法存根(返回值、抛异常、应答)
- Invocation counting and filtering - 调用计数和过滤
- Default value return for primitive types - 原始类型默认值返回
Usage Examples | 使用示例:
MockInvocationHandler handler = new MockInvocationHandler(UserService.class);
// Configure stubbing
handler.when("findById", 1L).thenReturn(new User("John"));
handler.when("save").thenAnswer(args -> args[0]);
handler.when("delete").thenThrow(new RuntimeException("Error"));
Security | 安全性:
- Thread-safe: Yes (CopyOnWriteArrayList for invocations, ConcurrentHashMap for stubs) - 线程安全: 是(调用使用CopyOnWriteArrayList,存根使用ConcurrentHashMap)
- Null-safe: Yes (validates mocked type) - 空值安全: 是(验证Mock类型)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStubbing configuration. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all invocations.intcountInvocations(String methodName) Counts invocations for method.intcountInvocations(String methodName, Object... args) Counts invocations for method with specific args.Gets all recorded invocations.getInvocations(String methodName) Gets invocations for method.Class<?> Gets the mocked type.voidreset()Resets handler (clears invocations and stubs).Sets up stubbing for method.
-
Constructor Details
-
MockInvocationHandler
Creates handler for mocked type. 为Mock类型创建处理器。- Parameters:
mockedType- the mocked type | Mock类型
-
-
Method Details
-
invoke
-
when
Sets up stubbing for method. 为方法设置存根。- Parameters:
methodName- the method name | 方法名args- the expected arguments (or empty for any) | 期望参数(或空表示任意)- Returns:
- the stubbing | 存根配置
-
getInvocations
Gets all recorded invocations. 获取所有记录的调用。- Returns:
- the invocations | 调用列表
-
getInvocations
Gets invocations for method. 获取方法的调用。- Parameters:
methodName- the method name | 方法名- Returns:
- the invocations | 调用列表
-
countInvocations
Counts invocations for method. 统计方法的调用次数。- Parameters:
methodName- the method name | 方法名- Returns:
- the count | 次数
-
countInvocations
-
clearInvocations
public void clearInvocations()Clears all invocations. 清除所有调用。 -
reset
public void reset()Resets handler (clears invocations and stubs). 重置处理器(清除调用和存根)。 -
getMockedType
-