By default, callouts aren’t allowed after DML operations in the same transaction because DML operations result in pending uncommitted work that prevents callouts from executing. Sometimes, you might want to insert test data in your test method using DML before making a callout. To enable this, enclose the portion of your code that performs the callout within Test.startTest and Test.stopTest statements. The Test.startTest statement must appear before the Test.setMock statement. Also, the calls to DML operations must not be part of the Test.startTest/Test.stopTest block.
DML operations that occur after mock callouts are allowed and don’t require any changes in test methods.
Performing DML Before Mock Callouts
This example is based on the previous example. The example shows how to use Test.startTest and Test.stopTest statements to allow DML operations to be performed in a test method before mock callouts. The test method (testEchoString) first inserts a test account, calls Test.startTest, sets the mock callout mode using Test.setMock, calls a method that performs the callout, verifies the mock response values, and finally, calls Test.stopTest.
1@IsTest2private with sharing class WebSvcCalloutTest{3 @IsTest4 static void testEchoString(){5 // Perform some DML to insert test data6 Account testAcct = new Account(Name = 'Test Account');7 insert as user testAcct;89 // Call Test.startTest before performing callout10 // but after setting test data.11 Test.startTest();1213 // Set mock callout class14 Test.setMock(WebServiceMock.class, new WebServiceMockImpl());1516 // Call the method that invokes a callout17 String output = WebSvcCallout.callEchoString('Hello World!');1819 // Verify that a fake result is returned20 Assert.areEqual('Mock response', output);2122 Test.stopTest();23}24}
Asynchronous Apex and Mock Callouts
Similar to DML, asynchronous Apex operations result in pending uncommitted work that prevents callouts from being performed later in the same transaction. Examples of asynchronous Apex operations are calls to future methods, batch Apex, or scheduled Apex. These asynchronous calls are typically enclosed within Test.startTest and Test.stopTest statements in test methods so that they execute after Test.stopTest. In this case, mock callouts can be performed after the asynchronous calls and no changes are necessary. But if the asynchronous calls aren’t enclosed within Test.startTest and Test.stopTest statements, you’ll get an exception because of uncommitted work pending. To prevent this exception, do either of the following:
Enclose the asynchronous call within Test.startTest and Test.stopTest statements.
1Test.startTest();2MyClass.asyncCall();3Test.stopTest();45Test.setMock(..); // Takes two arguments6MyClass.mockCallout();
Follow the same rules as with DML calls: Enclose the portion of your code that performs the callout within Test.startTest and Test.stopTest statements. The Test.startTest statement must appear before the Test.setMock statement. Also, the asynchronous calls must not be part of the Test.startTest/Test.stopTest block.
1MyClass.asyncCall();23Test.startTest();4Test.setMock(..); // Takes two arguments5MyClass.mockCallout();6Test.stopTest();
Asynchronous calls that occur after mock callouts are allowed and don’t require any changes in test methods.