I am facing this issue in a MockCallOut for http in test class. I know that a dml is not allowed just above the callout, so I have tried to put callout between Test.startTest() and Test.StopTest(); I have also tried doing the callout in future method. But, still I am unable to solve this problem. I need this dml as I need to insert data that is used in calling the method. Below is the abstract information about my implementation:
static testMethod void methodForMockCallOut(){
Account acc = new Account(name='Test Account');
insert acc;
Contact con= new Contact();
con.FirstName = 'Test';
con.LastName = 'User';
con.AccountId = acc.Id;
insert con;
test.startTest();
Test.setMock(HttpCalloutMock.class, new myMockCallOutClass());
MockCallOutForActualMethodToBeTested(con.id);
Test.stopTest();
}
@Future(Callout=true)
MockCallOutForActualMethodToBeTested(Id ContactId){
ClassToBeTested obj = new ClassToBeTested();
obj.actualMethodToBeTested(con.id);
}