Event and Event Bus Properties in Test Context
Test Events and the Test Event Bus
When an Apex test publishes an event message, it’s 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.
SELECT Name, Position, Retries, LastError FROM EventBusSubscriber WHERE 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.
Test Events and Limits
Event allocations don’t apply to test events, which have their own publishing limit of 500 event messages in a test method. If the number of event messages published from an Apex test context exceeds the limit, an error is returned with the LIMIT_EXCEEDED status code. The error is in the SaveResult that the EventBus.publish Apex method returns.
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, 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)
Apex tests don't cause CometD-based or Pub/Sub API subscribers to run.