Testing Your Platform Event in Apex

Add Apex tests to test platform event subscribers. Before you can package or deploy Apex code, including triggers, to production, it must have tests and sufficient code coverage. Add Apex tests to provide code coverage for your triggers.

  • Event and Event Bus Properties in Test Context

    In test context, event messages and the event bus have different properties. State information of events and subscribers is reset and isn’t persisted.

  • Deliver Test Event Messages

    Deliver test event messages after the Test.stopTest() statement. Alternatively, deliver test event messages at any time with the Test.getEventBus().deliver() method. Fail test event messages with the Test.getEventBus().fail() method.

  • Test Retried Event Messages

    An Apex trigger can retry processing of an event message by throwing EventBus.RetryableException. In API version 43.0 and later, you can test retried event messages by calling Test.EventBus.deliver() and inspecting EventBusSubscriber fields.

See Also