Sub topic 3
How Test.startTest and Test.stopTest work
This chapter assumes you can write a basic @IsTest class, use DML in Apex, and know what governor limits are. It covers the mechanics of the test window. Later chapters apply it to each async type.
The problem the window solves
Asynchronous Apex (future methods, Queueable jobs, Batch Apex, Schedulable classes) runs in its own transaction, after the code that started it has finished. A test method is a single transaction. If it queued a job and then queried the results, the job would not have run yet, and the assertion would fail or check stale data.
Test.startTest() and Test.stopTest() fix this. They mark a window around the code under test and change what the platform does at the end of that window.
What the two calls do
Test.startTest()marks the point where the code under test begins. Limits for the test method are saved, and the code after the call gets a fresh set of governor limits.Test.stopTest()marks the end of the window. Any async work queued between the two calls runs synchronously at this point. The call does not return until that work has finished. Then the limits from beforestartTestare restored.
Because the work runs inside stopTest, the next line of your test can see its results.
Where each part of the test goes
Place code in three zones:
- Before
startTest: create test data. This work uses the limits you had at the start, so heavy setup does not eat into the limits of the code under test. - Between the calls: run only the code under test, the call that queues the async work.
- After
stopTest: re-query the records and assert.
@IsTest
private class AccountServiceTest {
@IsTest
static void markReviewedUpdatesDescription() {
// 1. Setup, before the window
Account a = new Account(Name = 'Test Account');
insert a;
// 2. Code under test, inside the window
Test.startTest();
AccountService.markReviewed(new Set<Id>{ a.Id });
Test.stopTest();
// 3. Assertions, after the window
Account result = [SELECT Description FROM Account WHERE Id = :a.Id];
Assert.areEqual('Reviewed', result.Description);
}
}This test uses the AccountService.markReviewed future method from the Topic Spine's first example. The in-memory variable a still holds the old values after stopTest. The query is what reads the updated record, so always re-query.
Common mistakes
- Asserting before
stopTest. The job has not run yet, so the data is unchanged. - Calling
startTestorstopTestmore than once. Each test method can use only one pair, and a second call raises an error. - Putting setup inside the window. It runs under the fresh limits and hides how close the real code is to a limit.
- Forgetting to call
stopTest. The async work then does not run in the test, or runs only when the test ends, after your assertions.
Exercise
A test inserts an Account, calls a method that enqueues a Queueable job inside the test window, and asserts on the Account after the job. Where do the insert, the enqueue call and the assertion go, and why must the assertion query the record again?
Show answer
The insert goes before Test.startTest(), the enqueue call goes between startTest and stopTest, and the assertion goes after Test.stopTest(). The job runs synchronously when stopTest is called, and it updates the record in the database. The Account variable in the test still holds the old in-memory values, so the test must query the record again to see the change.