7

I keep getting a System.UnexpectedException whenever I run unit tests for code that publishes Platform Events. As soon as I call Test.stopTest(), the test fails with System.UnexpectedException: Error processing messages

I have even tried writing a very simple unit test which also fails:

@isTest
private static void samplePublishApplicationUpdateEvent_justPublishingAnEvent_expectToLearnSomething() {
    Application_Update__e updateEvent = new Application_Update__e();
    updateEvent.Application_Id__c = '12345';

    Test.startTest();
    EventBus.publish(updateEvent);
    Test.stopTest();
}

As soon as we hit Test.stopTest(), I receive the System.UnexpectedException failure.

The API version of the classes is 40. The user I am running the tests as has read/create permissions for my platform event.

Does anyone have any ideas?

  • The documentation found at developer.salesforce.com/docs/atlas.en-us.platform_events.meta/… seems to suggest you're doing everything correctly. I'm assuming the debug logs are not showing anything significant either? – Robert Watson Jul 18 '17 at 20:20
  • Do you have any Apex triggers subscribed to the event that might be failing? What do the debug logs show? – Daniel Ballinger Jul 18 '17 at 20:38
  • I was also going to suggest that you raise it on the Official Platform Events chatter channel, but it looks like you already have. – Daniel Ballinger Jul 18 '17 at 20:55
  • Thanks guys. It turns out we have a salesforce bug on our hands. Salesforce is currently working on patching the issue. I will post an answer with more details. – Zach Field Jul 21 '17 at 14:05
5

Answer: Salesforce bug

Workaround: Create a trigger for your platform events.

I reached out to the Official Platform Events community and created a case with Salesforce. This is a Salesforce bug which they are currently working on patching.

The reason my unit tests were failing is because I did not have a trigger for the platform event that was being published.

After creating a trigger for my platform event, the unit tests pass.

The trigger doesn't have any logic in it since our business case don't require it. Our platform event was only meant for external subscribers.

Update as of 7/31/2017: Salesforce has released a patch to avoid the need for this workaround. I was able to verify the fix in our sandbox org by deleting the dummy trigger and running my original unit tests.

That said, I have restored the "dummy trigger" since it is actually helpful for unit tests to make assertions against the events that are published.

  • dummy trigger to persist some object that you can query to see if Platform Event was published within a testmethod? If you have x Platform Event__e objects, that is x triggers. The right answer might be to exploit a common publisher method that you tie into Apex Mocks (that exploits Stub API) – cropredy Aug 11 '17 at 20:00
2

There is a new method coming in Summer '18 (v42.0) that to support testing Platform events.

Deliver Test Events on Demand

Use Test.getEventBus().deliver() to deliver test events in an Apex test. You no longer have to rely only on Test.stopTest() for event delivery at the end of a test. You can now deliver test events multiple times within a test and verify that subscribers have processed the test events each step of the way.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.