I've been doing a lot of work with Platform events and found it a fantastic way to implement some domain driven design patterns, decouple code, and separate execution contexts. Unfortunately I've been running into frequent gotchas, the most recent of which is in testing. Running code that, as a side effect, publishes platform events, or even directly publishes an event within Test.startTest(); and Test.stopTest(); consistently throws the following error:
System.UnexpectedException: Error processing messages Stack Trace Class.System.Test.stopTest: line 19, column 1
A test might look something like this:
... Example_Event__e exev = new Example_Event__e(); exev.Example_Date_Time__c = Datetime.now(); Test.startTest(); Database.SaveResult sr = EventBus.publish(exev); System.assertEquals(true, sr.isSuccess()); Test.stopTest();
These tests are being run on a developer sandbox on cs19.
Anyone know a workaround to getting my Trigger/Handler for this event to run in a test context? I've tried recompiling classes, and verified that all my platform event triggers and classes using platform events are on API version 42.0. I've also managed to deploy a Platform Event Trigger/Handler to production using a similar test, so I'm confused as to why I'm running into these errors now.
This error only occurs within Test.startTest(); and Test.stopTest(); however omitting the clauses doesn't run the code.
Log__c. You can verify the persistence of this object after Test.stopTest. V43 has better mechanisms as @pranayJaiswal points out