Sub topic 14
Testing change event triggers
This chapter assumes you can write Apex triggers and test classes, and that you know how change event triggers work from the previous chapter. It covers why change event triggers need special test steps and how to write a test that passes reliably.
Why ordinary tests fail
A change event trigger does not run when you insert or update a record. Salesforce publishes the change event to the event bus after the transaction commits, and the trigger runs in a separate, later transaction. In a test, that later step never happens on its own. If you assert right after update acct;, the trigger has not run yet and your assertion fails.
Two things fix this:
Test.enableChangeDataCapture()turns on change event publishing for the test. Without it, DML in a test does not produce change events, even if CDC is enabled for the object in your org.Test.getEventBus().deliver()delivers the published events to subscribers, including your trigger, so the trigger runs before your next line.
A complete test
The trigger from the previous chapter creates a Task when an Account update changes Phone. This test checks that behavior.
@isTest
private class AccountChangeTriggerTest {
@isTest
static void createsTaskWhenPhoneChanges() {
Test.enableChangeDataCapture();
Account acct = new Account(Name = 'Acme');
insert acct;
Test.getEventBus().deliver();
acct.Phone = '555-0100';
update acct;
Test.getEventBus().deliver();
System.assertEquals(1, [SELECT COUNT() FROM Task WHERE WhatId = :acct.Id]);
}
}Read it in order:
Test.enableChangeDataCapture()comes first, before any DML you want captured.- The first
deliver()flushes the CREATE event. The trigger seeschangeType == 'CREATE', so it does nothing. - The update changes
Phone, so the trigger findsPhoneinchangedFieldsand inserts one Task. - The second
deliver()runs the trigger for the UPDATE event. Only then does the assertion run.
Test the negative case too
A good test also proves the trigger stays quiet when it should. Add a method that updates a field other than Phone.
@isTest
static void ignoresChangesToOtherFields() {
Test.enableChangeDataCapture();
Account acct = new Account(Name = 'Acme');
insert acct;
Test.getEventBus().deliver();
acct.Description = 'Updated description';
update acct;
Test.getEventBus().deliver();
System.assertEquals(0, [SELECT COUNT() FROM Task WHERE WhatId = :acct.Id]);
}Put this method inside the same test class as the first one.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Assertion sees no Task | No `deliver()` call after the DML | Call `Test.getEventBus().deliver()` before asserting |
| Trigger never runs in the test | `Test.enableChangeDataCapture()` is missing or called after the DML | Call it first in the test method |
| Count is higher than expected | Extra events from earlier DML were delivered later | Deliver after each DML step and assert on the specific record |
| Test passes in one org, fails in another | Test relied on existing org data | Create all data inside the test |
What to cover
- Each
changeTypeyour trigger handles, such as CREATE, UPDATE, DELETE, and UNDELETE. - Updates that change a watched field and updates that do not.
- Bulk behavior: insert or update 200 records and confirm the trigger still makes one DML call and the right number of Tasks.
Exercise
Your test inserts an Account, updates its Phone, and immediately asserts that a Task exists. The assertion fails, even though the trigger works in the org. Which two test calls are likely missing?
Show answer
Test.enableChangeDataCapture() before the DML, so the test publishes change events, and Test.getEventBus().deliver() after the update, so the trigger runs before the assertion.
Exercise
Why does the sample test call deliver() after the insert, and not only after the update?
Show answer
The insert publishes a CREATE event. Delivering it right away runs the trigger for that event, so it is handled in its own batch. The later UPDATE delivery then involves only the UPDATE event, which keeps the assertion about Task count clear.