Sub topic 11
DML before callouts and Test.startTest
This chapter assumes you can write @IsTest classes, use Test.setMock with an HttpCalloutMock, and read the WeatherService class from earlier chapters. Here you will see why a test can fail with an "uncommitted work pending" error and how Test.startTest avoids it.
The uncommitted work rule
Apex does not allow a callout after DML in the same transaction. If you insert, update, or delete a record and then make a callout before the transaction commits, Apex throws a System.CalloutException:
You have uncommitted work pending. Please commit or rollback before calling out.The rule exists because a callout can run for a long time, and the platform does not hold database locks that long. A test is one transaction, so the rule applies to it too. Mocked callouts are still callouts. Test setup that inserts records can therefore break a test even though no real request is sent.
Why Test.startTest fixes it
Test.startTest() marks the point where the code under test begins. It gives that code a fresh set of governor limits. DML you ran before startTest does not block callouts made after it. The pattern is: create data, register the mock, call startTest, then run the code under test.
Data created in a @TestSetup method is already committed before each test method runs, so it never causes this error.
A worked example
This service makes the callout first and does its DML afterward. That is the safe order in production code as well.
public with sharing class AccountWeatherService {
public static void refresh(Id accountId, String city) {
String condition = WeatherService.getCondition(city);
update new Account(Id = accountId, Description = condition);
}
}The test inserts its data before startTest:
@IsTest
private class AccountWeatherServiceTest {
@IsTest
static void storesConditionOnAccount() {
Account acc = new Account(Name = 'Acme');
insert acc;
Test.setMock(HttpCalloutMock.class, new WeatherServiceMock(200, '{"condition":"Sunny"}'));
Test.startTest();
AccountWeatherService.refresh(acc.Id, 'Oslo');
Test.stopTest();
Account result = [SELECT Description FROM Account WHERE Id = :acc.Id];
Assert.areEqual('Sunny', result.Description, 'Description should hold the mocked condition');
}
}The insert acc runs before Test.startTest(), so the callout inside refresh is allowed. The update inside refresh runs after the callout, so it is also allowed.
What still fails
| Situation | Result |
|---|---|
| DML before `startTest`, callout after | Works |
| `@TestSetup` data, callout in the test | Works |
| Callout, then DML in the code under test | Works |
| DML in the code under test, then a callout | `CalloutException` |
The last row is a real design problem, not a test problem. If your production method updates a record and then calls out, the test will fail, and so will the method in production. Reorder the code so the callout comes first. If the order cannot change, move the callout into a Queueable that implements Database.AllowsCallouts, so it runs in its own transaction.
Exercise
A test inserts an Account, calls Test.startTest(), then inserts a Contact, and then calls a method that makes a mocked callout. The test fails with "You have uncommitted work pending." What is the cause, and how do you fix the test?
Show answer
The Contact insert is DML that happens after startTest and before the callout, so the transaction has uncommitted work when the callout runs. Move the Contact insert above Test.startTest(), next to the Account insert. The callout then runs with no uncommitted DML in the code-under-test context.