Newer Version Available
Test Change Event Triggers
Enable All Change Data Capture Entities for Notifications
To enable the generation of change event notifications for all supported Change Data Capture entities for an Apex test, call this method.
Call the Test.enableChangeDataCapture() method at the beginning of your test method before performing DML operations and calling Test.getEventBus().deliver() or Test.stopTest().
The Test.enableChangeDataCapture() method ensures that Apex tests can fire change event triggers regardless of the entities selected in Setup. This method doesn’t affect the Change Data Capture entity selections for the org.
Deliver Test Change Events
To test your change event trigger, perform DML operations and then call the Test.getEventBus().deliver() method. The method delivers the event messages from the test event bus to the corresponding change event trigger and causes the trigger to fire. Finally, validate that the trigger executed as expected. For example, if the trigger creates or updates records, you can query those records with SOQL.
This test method outlines the order of statements that must be executed in a test, starting with enabling Change Data Capture entities.
Alternatively, use the Test.startTest(), Test.stopTest() method block to fire a change event trigger. After Test.stopTest() executes, all test change event messages generated from DML operations are delivered to the associated trigger. The DML statements can be within the block or outside the block as long as they precede Test.stopTest().
Apex Test Example Based on Quick Start Trigger
The testNewAccount method in this test class shows you how to write a test for the MyAccountChangeTrigger trigger provided in the Add an Apex Trigger quick start. The test method first enables all entities for change notifications. It creates a test account and then calls the Test.getEventBus().deliver(); method. Next, the test verifies that the trigger’s execution by querying Task records and validating that one task was created. The query returns only tasks that the trigger created in test contest. For that reason, the test expects only one task. Next, the test updates the account and verifies that no new task is created.
This test class is an alternative example that uses the Test.startTest(), Test.stopTest() method block to deliver test change events and fire the trigger. For more information about these methods, see Using Limits, startTest, and stopTest in the Apex Developer Guide.
Properties of Change Events in Test Context
Test change events messages are published to the test event bus, which is separate from the Salesforce event bus. They aren’t persisted in Salesforce and aren’t delivered to event channels outside the test class. Properties of test change event messages, like the replay ID, are reset in test context and reflect only the values of test event messages. For more information, see Event and Event Bus Properties in Test Context in the Platform Events Developer Guide.