Since platform event triggers are run asynchronously, an error in processing them doesn't cause the test method to fail by itself.

So, how would I write a test that under certain circumstances, my PE trigger should do nothing and not cause an unhandled exception?

The specific scenario I want to test is when the data my platform event is supposed to process has been removed between the platform event firing and the trigger running. It does a query to make sure the data is still available, so it ought to work, but I want a test in place to prove it.

Here's what I tried so far (in the last part of my test):

Id triggerId = [SELECT Id FROM ApexTrigger WHERE Name = 'MyTriggerName'].Id;
EventBusSubscriber eventBusSubscriber = [SELECT Retries, Status FROM EventBusSubscriber WHERE ExternalId = :((String)triggerId).substring(0, 15)];
System.assertEquals('Running', eventBusSubscriber.Status);
System.assertEquals(0, eventBusSubscriber.Retries);

If I force my trigger to throw an exception, this passes, even though I can see from the debug log that my trigger did throw the exception.

Is there any way to check this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.