Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

TestBroker Class

Contains methods that simulate the successful delivery or failed publishing of platform event or change event messages in an Apex test.

Namespace

EventBus

TestBroker Methods

The following are methods for TestBroker.

deliver()

Delivers platform event messages to the test event bus. Use this method to deliver test event messages multiple times and verify that event subscribers have processed the test events each step of the way.

Signature

public void deliver()

Return Value

Type: void

Usage

Enclose Test.getEventBus().deliver() within the Test.startTest() and Test.stopTest() statement block.

1Test.startTest();
2// Create test events
3// ...
4// Publish test events with EventBus.publish()
5// ...
6// Deliver test events
7Test.getEventBus().deliver();
8// Perform validation 
9// ...
10Test.stopTest();

fail()

Causes the publishing of platform event messages to fail in the test event bus. Use this method to test Apex publish callbacks.

Signature

public void fail()

Return Value

Type: void

Usage

1// Create test events
2// ...
3// Publish test events with EventBus.publish()
4// ...
5// Fail publishing of test events
6Test.getEventBus().fail();
7// Perform validation 
8// ...

For more information, see <link>Get the Result of Asynchronous Platform Event Publishing with Apex Publish Callbacks<link/> in the Platform Events Developer Guide.