3

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’][0];

                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'][0];
                
    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.

2
  • You need to provide code snippets around how the flag is set and how it is read, making the two contexts explicitly clear. Please edit the question to add detail.
    – Phil W
    Commented Jan 30, 2023 at 21:32
  • Thanks for the update.
    – Phil W
    Commented Feb 6, 2023 at 15:03

1 Answer 1

0

This answer is a bit opinionated (or at least based on assumptions) since I've no access to the underlying platform code. We hit a similar issue.

I agree, this is very likely related to the static variable handling "correction". I'd suggest the code was updated to reset statics before (rather than "between") each "group", i.e. before the very first one too. This seems appropriate since the platform event subscriber actually runs in a separate transaction.

Your unit test was relying on a bug and needs to be refactored.

It rather depends on what you are trying to get coverage against, and how your flow-based subscriber is structure, but I'd consider directly invoking the processing that is invoked by it from your unit test.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .