3

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.

1
  • We test our V42 PEs by having a trigger or PB subscribe to the event and said trigger/PB persists to a custom object Log__c. You can verify the persistence of this object after Test.stopTest. V43 has better mechanisms as @pranayJaiswal points out
    – cropredy
    May 1, 2018 at 19:06

2 Answers 2

3

Platform events in test class are unitested differently.

Instead of EventBus.publish(exev); you have to use Test.getEventBus().deliver();

Test.startTest();
// Create test events
// ...
// Publish test events with EventBus.publish()
// ...
// Deliver test events
Test.getEventBus().deliver();
// Perform validation 
// ...
Test.stopTest();

Make sure you put Test.getEventBus().deliver(); between start and stop test.

https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_platform_events_apex_enhancements_deliver_events.htm This method is only Available after Summer 18. or API v43

For Version 42 and Below:

Create a blank Trigger for your Platform Event Object, SRC:.Unable to unit test code that publishes platform events

4
  • Do you know a way around this in API v42? I've looked through the Summer docs and hopefully the changes resolve a lot of the issues I've been seeing in testing. The concerning thing to me is that I've successfully deployed and ran my event triggers without running into this error before.
    – zWhiteD
    May 1, 2018 at 17:55
  • updated answer.. it looks like a similar issue. May 1, 2018 at 17:58
  • Hmm I looked through that, but I figured it would be in a different issue since it was resolved. I'll try adding another trigger for the event my test code is also emitting.
    – zWhiteD
    May 1, 2018 at 18:04
  • Still getting the same error with the additional trigger for the other event published in the test. I did an EventBusSubscriber query and confirmed that the triggers are readily handling the events.
    – zWhiteD
    May 1, 2018 at 18:14
1

I had a similar issue and resolved it with V43.0

This works fine

// Given a list of Platform Events of various types
Sobject[] platEvents = new List<Sobject> {
        new Foo__e(),
        new Bar__e()
    };

// When submitted to the EventBus 1x1
Test.startTest();
for (Sobject platEvent : platEvents) {
    EventBus.publish(platEvent);
    Test.getEventBus().deliver();
}
Test.stopTest();

// Then do asserts ... 

However, this throws a gack

// Given a list of Platform Events of various types
Sobject[] platEvents = new List<Sobject> {
        new Foo__e(),
        new Bar__e()
    };

// When submitted to the EventBus as a list
Test.startTest();
EventBus.publish(platEvents);
Test.getEventBus().deliver();  // throws gack 512079400-52679 (324305352) (324305352)
Test.stopTest();

// Then do asserts ... 

You must log in to answer this question.

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