I have a problem with FeedItems in my Test class, it's very weird behaviour Does someone explain to me why this happens?
Callout Class
public with sharing class Callout {
public static void runProccess(Case[] records) {
Database.executeBatch(new CalloutBatch(records), 10);
}
public static void send(Case[] records) {
Case[] toUpdate = new Case[]{};
for (case record : records) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:test/SOME_URL');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody('');
Http http = new Http();
HTTPResponse res = http.send(req);
Map<String, Object> response = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
toUpdate.add(new Case(Id = (Id) response.get('Id'), Status = (String) response.get('Status')));
}
update toUpdate;
}
}
CalloutBatch Class
global with sharing class CalloutBatch implements Database.Batchable<Case>, Database.AllowsCallouts{
private final Case[] records;
global CalloutBatch (Case[] records) {
this.records = records;
}
global Iterable<Case> start(Database.BatchableContext BC) {
return records;
}
global void execute(Database.BatchableContext BC, Case[] scope) {
Callout.send(scope);
}
global void finish(Database.BatchableContext BC) {
}
}
Test Class
@IsTest
private class CalloutTest {
@IsTest
static void shouldReaciveData() {
String status = 'Closed';
Case caseRecord = new Case(Type = 'Web', Status = 'New');
insert caseRecord;
//insert new FeedItem(ParentId = caseRecord.Id, Visibility='AllUsers', Type = 'TextPost', Body = '');
System.assertEquals(0, [SELECT Count() FROM Case WHERE Id = :caseRecord.Id AND Status = :status]);
Test.startTest();
Test.setMock(HttpCalloutMock.class, new CalloutTestMock(caseRecord.Id, status));
Callout.runProccess(new Case[]{caseRecord});
Test.stopTest();
System.assertEquals(1, [SELECT Count() FROM Case WHERE Id = :caseRecord.Id AND Status = :status]);
}
}
Mock Class
public with sharing class CalloutTestMock implements HttpCalloutMock {
private Id recordId;
private String status;
public CalloutTestMock(Id recordId, String status) {
this.recordId = recordId;
this.status = status;
}
public HTTPResponse respond(HTTPRequest req) {
HttpResponse response = new HttpResponse();
response.setStatusCode(200);
response.setBody('{"Id": "' + recordId + '", "Status": "' + status + '"}');
return response;
}
}
This code works, but when I uncomment insert FeedItem in Test Class I receive an error message:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out Class.Callout.send: line 23, column 1 Class.CalloutBatch.execute: line 20, column 1
FeedItem doesn't have an impact on this functionality, (I don't have any Process Builders, triggers etc. on FeedItem object)