Sub topic 13
Delivering Platform Events with Test.getEventBus().deliver()
This chapter assumes you can write an @IsTest class, publish records with EventBus.publish, and read a simple Apex trigger. You should also know the startTest/stopTest window from earlier chapters.
Why events need their own step
A platform event is delivered asynchronously. In a test, publishing an event does not run its subscribers right away. Code that subscribes to the event, such as an after insert trigger on the event object, stays idle until the event is delivered.
Test.stopTest() does deliver events that were published inside the test window. Test.getEventBus().deliver() lets you deliver them at a point you choose. This helps when you want to assert in the middle of a test, or when you publish events outside a startTest/stopTest pair.
A worked example
The event object Order_Event__e has a text field Account_Id__c. A trigger on it marks the matching account.
trigger OrderEventTrigger on Order_Event__e (after insert) {
List<Account> toUpdate = new List<Account>();
for (Order_Event__e e : Trigger.new) {
toUpdate.add(new Account(Id = e.Account_Id__c, Description = 'Order received'));
}
update toUpdate;
}The test publishes the event, delivers it, and then re-queries the account.
@IsTest
private class OrderEventTriggerTest {
@IsTest
static void deliveredEventUpdatesAccount() {
Account a = new Account(Name = 'Event Test');
insert a;
Test.startTest();
Database.SaveResult sr = EventBus.publish(
new Order_Event__e(Account_Id__c = a.Id)
);
Assert.isTrue(sr.isSuccess());
Test.getEventBus().deliver();
Test.stopTest();
Account result = [SELECT Description FROM Account WHERE Id = :a.Id];
Assert.areEqual('Order received', result.Description);
}
}Check the SaveResult first. If publishing failed, for example because a required field is missing, no event reaches the bus and the later assertion fails for a confusing reason.
Where to call deliver
Call deliver() after EventBus.publish and before you assert. You can also call it before stopTest() to check state at an intermediate step. For example, you can assert that the first subscriber ran before the test publishes a second event.
If you publish and then call only stopTest(), delivery still happens. Using deliver() explicitly makes the intent clear and works even when the test has no window.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Assertion sees the old value | Assertion ran before delivery | Call `Test.getEventBus().deliver()` first |
| Subscriber never runs | Publish failed | Check `SaveResult.isSuccess()` |
| Stale record in memory | Asserting on the in-memory object | Re-query the record |
| Event not seen by a trigger | Test queried the event object | Events are not stored for querying. Assert on side effects |
Key points
- Publishing an event only queues it in the test.
Test.getEventBus().deliver()runs the subscribers on demand.Test.stopTest()also delivers events published in the window.- Always assert on records the subscriber changed, after re-querying them.
Exercise
A test publishes an OrderEvent_e and immediately asserts that an Account was updated by the event trigger. The assertion fails, though the trigger is correct. What one line fixes the test, and where does it go?
Show answer
Add Test.getEventBus().deliver(); after the EventBus.publish call and before the assertion. Without it the trigger has not run yet. Re-query the account after the call so the assertion reads the saved value.
Exercise
Why check Database.SaveResult.isSuccess() after EventBus.publish in a test?
Show answer
If the publish fails, no event is queued and the subscriber never runs. The failed publish would show up only as a failing assertion on the side effect, which hides the real cause.