10

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?

4
  • 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? Jul 18, 2017 at 20:20
  • Do you have any Apex triggers subscribed to the event that might be failing? What do the debug logs show? Jul 18, 2017 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. Jul 18, 2017 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, 2017 at 14:05

2 Answers 2

7

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();
6

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.

1
  • 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, 2017 at 20:00

You must log in to answer this question.

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