function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ken Koellner @ EngagewareKen Koellner @ Engageware 

Test Method Apex Trigger on Change Data Capture on Delete DML

I'm working with Change Date Capture and have a trigger EventChangeEvent (after insert) so I can handle the change in Apex.

My test method works fine with "insert Event".  The change is delivered and the trigger is fired and all downstream processing in Apex (including a subsequent Queueable) works fine.

My test method for delete isn't working.  The Change Event is never delivered to the trigger.  The @setup method sets up the data I want to delete in the test.  The logic is --

Event myEvent = [ SOQL query to get Event that was inserted in @setup];

Test.enableChangeDataCapture();
Test.startTest();

delete sfEvent;

Test.getEventBus().deliver();

Test.stopTest();

The reason there is both a deliver() and a stopTest() is that the deliver should cause the Change record to be delivered.  The stopTest() will fire a Queueable.   I used that exact pattern in my insert test and it works fine.

My insert test has logic like shown below and it works fine.  First the change event is delivered to the trigger and then the queueble runs.  (If I don't put in the deliver() call, things run out of order and the result is incorrect.)

Test.enableChangeDataCapture();
Test.startTest();

Event sfEvent = new Event (fill in fields in Event);
insert sfEvent;

Test.getEventBus().deliver();

Test.stopTest();