---
schema_version: "1"
id: 21c9e509c963307512cfd515c19b7d49588b502dfbc3e73c6abac4f105b1db73
title: TestBroker Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_eventbus_TestBroker.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_eventbus_TestBroker.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 873c4e47a3449dcc656e04c44966957e36759beba757b13fb488e0ceff63c6f3
description: Contains methods that simulate the successful delivery or failed publishing of platform event or change event messages in an Apex test.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.Causes the publishing of platform event messages to fail in the test event bus. Use this method to test Apex publish callbacks.
---
# 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_eventbus.htm "The EventBus namespace provides classes and methods for platform events and Change Data Capture events.")

- **[TestBroker Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_eventbus_TestBroker.htm#apex_eventbus_TestBroker_methods)**

## TestBroker Methods

The following are methods for TestBroker.

- **[deliver()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_eventbus_TestBroker.htm#apex_eventbus_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.
- **[fail()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_eventbus_TestBroker.htm#apex_eventbus_TestBroker_fail)**  
    Causes the publishing of platform event messages to fail in the test event bus. Use this method to test Apex publish callbacks.

### 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.

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

#### See Also

- [_Platform Events Developer Guide_](https://developer.salesforce.com/docs/atlas.en-us.264.0.platform_events.meta/platform_events/platform_events_intro.htm "Platform Events Developer Guide - HTML (New Window)")

### 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

// Create test events
// ...
// Publish test events with EventBus.publish()
// ...
// Fail publishing of test events
Test.getEventBus().fail();
// Perform validation 
// ...

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