Newer Version Available

This content describes an older version of this product. View Latest

Test Your Platform Event Trigger in Apex

Ensure that your platform event trigger is working properly by adding an Apex test. Before you can package or deploy any Apex code (including triggers) to production, your Apex code must have tests and sufficient code coverage. To publish platform events in an Apex test, enclose the publish statements within Test.startTest() and Test.stopTest() statements.

Call the publish method within the Test.startTest() and Test.stopTest() statements. In test context, the publish method enqueues the publish operation. The Test.stopTest() statement causes the event publishing to be carried out. Include your validations after the Test.stopTest() statement.

1// Create test events
2Test.startTest();
3// Publish test events 
4Test.stopTest();
5// Perform validation here

You can publish up to 500 events in a test method.

Example

This sample test class contains two test methods. The testValidEvent method checks that publishing an event is successful and fires the associated trigger. The testInvalidEvent method verifies that publishing an event with a missing required field fails, and no trigger is fired. The testValidEvent method creates one Low_Ink__e event. After Test.stopTest(), it executes a SOQL query to verify that a case record is created, which means that the trigger was fired. The second test method follows a similar process but for an invalid test.

This example requires that the Low_Ink__e event and the associated trigger is defined in the org.

1@isTest
2public class EventTest {
3    @isTest static void testValidEvent() {
4        
5        // Create a test event instance
6        Low_Ink__e inkEvent = new Low_Ink__e(Printer_Model__c='MN-123', 
7                                             Serial_Number__c='10013', 
8                                             Ink_Percentage__c=0.15);
9        
10        Test.startTest();
11        
12        // Publish test event
13        Database.SaveResult sr = EventBus.publish(inkEvent);
14            
15        Test.stopTest();
16                
17        // Perform validations here
18        
19        // Verify SaveResult value
20        System.assertEquals(true, sr.isSuccess());
21        
22        // Verify that a case was created by a trigger.
23        List<Case> cases = [SELECT Id FROM Case];
24        // Validate that this case was found
25        System.assertEquals(1, cases.size());
26    }
27    
28    @isTest static void testInvalidEvent() {
29        
30        // Create a test event instance with invalid data.
31        // We assume for this test that the Serial_Number__c field is required.
32        // Publishing with a missing required field should fail.
33        Low_Ink__e inkEvent = new Low_Ink__e(Printer_Model__c='MN-123',  
34                                             Ink_Percentage__c=0.15);
35        
36        Test.startTest();
37        
38        // Publish test event
39        Database.SaveResult sr = EventBus.publish(inkEvent);
40            
41        Test.stopTest();
42                
43        // Perform validations here
44        
45        // Verify SaveResult value - isSuccess should be false
46        System.assertEquals(false, sr.isSuccess());
47        
48        // Log the error message
49        for(Database.Error err : sr.getErrors()) {
50            System.debug('Error returned: ' +
51                        err.getStatusCode() +
52                        ' - ' +
53                        err.getMessage()+' - '+err.getFields());
54        }
55        
56        // Verify that a case was NOT created by a trigger.
57        List<Case> cases = [SELECT Id FROM Case];
58        // Validate that this case was found
59        System.assertEquals(0, cases.size());
60    }
61}