Newer Version Available
Event and Event Bus Properties in Test Context
In test context, event messages and the event bus have different properties. State
information of events and subscribers is reset and is not persisted.
Test Events and the Test Event Bus
When an Apex test publishes an event message, it is published to a test event bus that is separate from the Salesforce event bus. In an Apex test, state information of events and subscribers is reset, as follows.
- The event replay ID value is reset to 0 and starts from 1 for the first test event message.
- Event state information in EventBusSubscriber is reset. The last processed replay ID (EventBusSubscriber.Position) and the last published replay ID (EventBusSubscriber.Tip) are reset to 0.
- When test events are published and processed in subscribers, event state information is updated.
- Subscriber status is reset to Running (EventBusSubscriber.Status).
- You can query EventBusSubscriber to get event state. For example, the following SOQL query
gets some information about all trigger subscribers to the Order_Event__e
event.
1SELECT Name, Position, Retries, LastError 2FROM EventBusSubscriber 3WHERE Topic='Order_Event__e' AND Type='ApexTrigger'
After an Apex test finishes executing, state information of events and subscribers reverts to the non-test values.
In addition, event allocations don’t apply to test events, which have their own publishing limit of 500 event messages in a test method.
Testing Event Subscribers
Use an Apex test to test publishing and subscribing to a platform event. When you publish an event message in an Apex test, all event subscribers are notified and start execution, including:
- Apex triggers
- Processes (when using an Apex test class saved with API version 43.0 or later)
- Flows (when using in an Apex test class saved with API version 43.0 or later)