Class EventCaptor<E extends Event>
java.lang.Object
cloud.opencode.base.event.testing.EventCaptor<E>
- Type Parameters:
E- the type of event to capture | 要捕获的事件类型
- All Implemented Interfaces:
EventListener<E>
Event Captor - Test utility for capturing and asserting events
事件捕获器 - 用于捕获和断言事件的测试工具
Provides a convenient way to capture events in unit tests, wait for async events, and make assertions on captured events.
提供在单元测试中捕获事件、等待异步事件和对捕获的事件进行断言的便捷方式。
Features | 主要功能:
- Event capture list - 事件捕获列表
- Await support for async events - 支持等待异步事件
- First/last event accessors - 首个/最后事件访问器
- Reset for test isolation - 重置以隔离测试
Usage Examples | 使用示例:
EventCaptor<MyEvent> captor = new EventCaptor<>();
eventBus.subscribe(MyEvent.class, captor);
eventBus.publish(new MyEvent("test"));
assertThat(captor.count()).isEqualTo(1);
assertThat(captor.getFirst()).isNotNull();
assertThat(captor.getLast().getData()).isEqualTo("test");
// For async events
captor.reset();
eventBus.publishAsync(new MyEvent("async"));
assertThat(captor.awaitEvent(Duration.ofSeconds(5))).isTrue();
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-event V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitEvent(Duration timeout) Wait for at least one event to be captured 等待至少一个事件被捕获booleanawaitEvents(int count, Duration timeout) Wait for a specific number of events to be captured 等待捕获特定数量的事件intcount()Get the number of captured events 获取捕获的事件数量Get all captured events 获取所有捕获的事件getFirst()Get the first captured event 获取第一个捕获的事件getLast()Get the last captured event 获取最后一个捕获的事件booleanCheck if any events were captured 检查是否捕获了任何事件voidHandle an event 处理事件voidreset()Reset the captor, clearing all captured events 重置捕获器,清除所有捕获的事件
-
Constructor Details
-
EventCaptor
public EventCaptor()Create a new event captor 创建新的事件捕获器
-
-
Method Details
-
onEvent
Description copied from interface:EventListenerHandle an event 处理事件- Specified by:
onEventin interfaceEventListener<E extends Event>- Parameters:
event- the event to handle | 要处理的事件
-
getCapturedEvents
-
getFirst
Get the first captured event 获取第一个捕获的事件- Returns:
- the first event, or null if none captured | 第一个事件,如果没有捕获则为 null
-
getLast
Get the last captured event 获取最后一个捕获的事件- Returns:
- the last event, or null if none captured | 最后一个事件,如果没有捕获则为 null
-
count
public int count()Get the number of captured events 获取捕获的事件数量- Returns:
- the count of captured events | 捕获的事件数量
-
hasCaptured
public boolean hasCaptured()Check if any events were captured 检查是否捕获了任何事件- Returns:
- true if at least one event was captured | 如果至少捕获了一个事件返回 true
-
awaitEvent
Wait for at least one event to be captured 等待至少一个事件被捕获- Parameters:
timeout- maximum time to wait | 最大等待时间- Returns:
- true if an event was captured within timeout | 如果在超时内捕获了事件返回 true
- Throws:
NullPointerException- if timeout is null | 如果 timeout 为 null
-
awaitEvents
Wait for a specific number of events to be captured 等待捕获特定数量的事件- Parameters:
count- the number of events to wait for | 等待的事件数量timeout- maximum time to wait | 最大等待时间- Returns:
- true if the required count was reached within timeout | 如果在超时内达到所需数量返回 true
- Throws:
IllegalArgumentException- if count is less than 1 | 如果 count 小于 1NullPointerException- if timeout is null | 如果 timeout 为 null
-
reset
public void reset()Reset the captor, clearing all captured events 重置捕获器,清除所有捕获的事件
-