I am using platform events and in one of the after triggers I am calling a method in a class where the method is annotated with @Future(Callout=true)
In my test method below I am doing some DML to insert an account and opportunity record before I call Test.startTest(). My code is below, but I keep getting the "You have uncommitted work pending" error.
//insert dml here.. List<Job_Posting__e> jobPostingList = new List<Job_Posting__e>(); Job_Posting__e jobPosting = new Job_Posting__e(); jobPosting.jobTitle__c = 'Java Developer'; jobPosting.jobLocation__c = 'Raleigh, NC'; jobPostingList.add(jobPosting); Test.startTest(); Test.setMock(HttpCalloutMock.class, new ServiceHttpResponseMock()); List<Database.SaveResult> results = EventBus.publish(jobPostingList); Test.stopTest();
If I move the the line with EventBus.publish(jobPostingList) after Test.stopTest() the error goes away, but I think that line should be within the Test.start() and Test.stopTest() shouldn't it?
How can I resolve this error given the code above?