DML operation INSERT not allowed on STANDARD PLATFORM EVENT

By | December 12, 2022

You can work around this error by running the test class in a system context.

There might possibly be another way to solve the problem without changing the contex, but I did not pursue that in depth.

Below is an example code.

@isTest
private without sharing class B2B_OrderSummaryCreatedEventTriggerTest {
    @isTest(seeAllData = true) // For the connect api to work, seeAllData must be set to true.
    private static void B2B_OrderSummaryCreatedEventTriggerTest() {
        OrderSummary os = B2B_TestDataFactory.createOrderSummary();
        OrderSummaryCreatedEvent osce = B2B_TestDataFactory.createOrderSummaryStandardPlatformEvent(os);
        Test.startTest();
        Database.SaveResult sr = EventBus.publish(osce);
        Test.stopTest();      
    }
}