As of Spring 23, several tests where we publish platform events started failing. We use a static flag as a condition to execute a code block and looks like now the line where the flag is being set is ignored in the tests. Even though it is being set and the events are being published inside Test.startTest() and Test.stopTest(). I also saw this other question with a similar issue Spring 23 Platform Event tests broken It feels like it is related to this change https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_static_variables_interviews.htm&release=242&type=5 If this change really is the reason, why the static variable is being reset in tests? It is not a flow and there is no user context change. We fixed the tests by not using a static flag in the code. But I would still like to understand why is it happening and how can it affect the processes we have. We do not use flows, only triggers. One example would be this test:
// list of events (eventsToPublish) is created Test.startTest(); MyApexClass.THROW_ERROR_TEST = true; Eventbus.publish(eventsToPublish); Test.getEventBus().deliver(); //First Event will Fail EventBusSubscriber subs1 = [SELECT Name, Position FROM EventBusSubscriber WHERE Type = 'ApexTrigger' AND Topic=‘my_platform_event__e’]; MyApexClass.THROW_ERROR_TEST = false; Test.getEventBus().deliver(); // Next batch execution (with the second Event only) EventBusSubscriber subs2 = [SELECT Name, Position FROM EventBusSubscriber WHERE Type = 'ApexTrigger' AND Topic='my_platform_event__e']; Test.stopTest(); // asserts
Here we use a static variable to throw an error during test execution. When the event is published the trigger on this event is executed. This test was working for a couple of years before the Spring 23 release. Reading the logs the first
MyApexClass.THROW_ERROR_TEST = true;
just does not appear. That line is skipped. Surrounding this line with debugs does not show the debugs in logs.