Sub topic 12
Testing event publishing and subscribers
This chapter assumes you can write basic Apex tests and that you know how EventBus.publish and platform event triggers work. You will learn why event tests behave differently from ordinary DML tests, and how to check both the publisher and the subscriber.
Why event tests need special handling
Platform events are delivered asynchronously. When your code calls EventBus.publish, the event goes onto the event bus, and the subscriber trigger runs later in its own transaction. A test can't just publish and then query the results, because the trigger may not have run yet.
Apex tests solve this with two methods. Test.startTest() and Test.stopTest() bracket the code under test. When Test.stopTest() runs, Salesforce delivers the events published since Test.startTest() to their subscribers. After that line, you can query for the records the trigger created.
The flow below shows the order of events.
Testing a publisher and its subscriber
The test below publishes one Order_Shipped__e event. It then checks two things: the publish call succeeded, and the trigger created the expected Task.
@IsTest
private class OrderShippedTriggerTest {
@IsTest
static void createsTaskWhenEventIsDelivered() {
Test.startTest();
Database.SaveResult sr = EventBus.publish(
new Order_Shipped__e(Order_Number__c = '00012345', Status__c = 'Shipped')
);
Test.stopTest();
System.assert(sr.isSuccess(), 'The event should be published');
System.assertEquals(1, [SELECT COUNT() FROM Task WHERE Subject = 'Follow up on order 00012345']);
}
}Note where each check sits:
sr.isSuccess()tells you Salesforce accepted the event for publishing. It says nothing about the subscriber.- The
SELECT COUNT()query comes afterTest.stopTest(). If you move it before, it returns 0 because the trigger has not run.
Testing your own publishing code
Often the event is published by a class you wrote, such as a service method that publishes when an order changes. In that case, call the service between startTest and stopTest. Then assert on the outcome in the same way. If the publisher is meant to publish nothing under some conditions, assert that no Task exists after stopTest.
Testing bulk delivery
Subscriber triggers receive events in batches in Trigger.new. A trigger that works for one event can still fail with many. Publish a list of events in one call to check that the trigger is bulk safe.
@IsTest
private class OrderShippedBulkTest {
@IsTest
static void handlesManyEvents() {
List<Order_Shipped__e> events = new List<Order_Shipped__e>();
for (Integer i = 0; i < 200; i++) {
events.add(new Order_Shipped__e(
Order_Number__c = 'ORD-' + i,
Status__c = 'Shipped'
));
}
Test.startTest();
List<Database.SaveResult> results = EventBus.publish(events);
Test.stopTest();
for (Database.SaveResult sr : results) {
System.assert(sr.isSuccess(), 'Every event should be published');
}
System.assertEquals(200, [SELECT COUNT() FROM Task WHERE Subject LIKE 'Follow up on order ORD-%']);
}
}Common mistakes
- Asserting before
Test.stopTest(). The trigger has not run yet, so the assertion sees no results. - Checking only
isSuccess(). A successful publish does not prove the subscriber did the right thing. Always assert on the records the trigger creates. - Testing with one event only. Bulk problems such as DML or SOQL inside a loop only show up with many events.
- Querying the event object. Platform events are not stored like records, so you can't verify them with SOQL. Verify the effects instead.
Exercise
Exercise
A test publishes an OrderShipped_e event, then immediately queries for the Task the trigger should create, and the count is 0. The trigger code is correct. What is the likely cause, and how do you fix it?
Show answer
The test queries before the event is delivered. Events published in a test are delivered to subscribers when Test.stopTest() runs. Wrap the publish call in Test.startTest() and Test.stopTest(), and run the query after Test.stopTest().
Exercise
Why should a subscriber test publish a list of events instead of one?
Show answer
The trigger receives events in batches in Trigger.new, so a test with a single event can't expose bulk problems such as SOQL or DML inside a loop. Publishing many events, for example 200, checks that the trigger stays within governor limits.