Sub topic 10
Asserting results after stopTest
This chapter assumes you can write a basic @IsTest class, run DML on SObjects, and have a general idea of what @future, Queueable, Batch and Schedulable Apex do. It covers where assertions go in an async test and how to write assertions that still hold when the async job changes data.
Why assertions belong after stopTest
Async Apex runs in its own transaction. When your test calls an async method between Test.startTest() and Test.stopTest(), the platform holds the job back. It runs the job synchronously when Test.stopTest() is reached. An assertion placed before stopTest therefore checks data the job has not touched yet.
Re-query, do not reuse variables
The job updates records in the database. It does not update the Apex variables your test holds. If you inserted Account a before the window, a.Description still has its old value after stopTest. Query the record again and assert on the result.
@IsTest
private class AccountServiceTest {
@IsTest
static void markReviewedUpdatesDescription() {
Account a = new Account(Name = 'Test Account');
insert a;
Test.startTest();
AccountService.markReviewed(new Set<Id>{ a.Id });
Test.stopTest();
// a.Description is still null here. Query the saved record.
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 earlier chapter on @future methods. The assertion runs after stopTest, on a fresh query.
Choose what to assert
Pick the observable result of the job. The right thing depends on the async type.
| Async type | What to assert after stopTest |
|---|---|
| `@future` | Field values or new records the method wrote |
| Queueable | Field values or new records, plus the job Id returned by `System.enqueueJob` is not null |
| Batch | Count or values of records in the processed scope |
| Schedulable | The `CronTrigger` record, or the effects of calling `execute` directly |
For a Batch job, a count query keeps the assertion short and checks every record at once:
Integer processed = [SELECT COUNT() FROM Account WHERE Description = 'Batch processed'];
Assert.areEqual(5, processed);Write assertions that can fail
An assertion that cannot fail proves nothing. Two habits help.
- Set up data so that the starting state differs from the expected state. If the job writes
'Reviewed', the account must not already hold'Reviewed'. - Assert on specific values with
Assert.areEqual, not only on "something was returned". Add a message as the last argument so a failure explains itself.
Assert.areEqual(
'Processed',
[SELECT Description FROM Account WHERE Id = :a.Id].Description,
'The Queueable job should set the description'
);Common mistakes
- Asserting inside the window. Code between
startTestandstopTestruns before the job. Move the assertion belowstopTest. - Asserting on a stale variable. Re-query by Id instead of reading the object you inserted.
- Asserting only that nothing threw. A job can finish without changing anything, for example when its query matched no rows. Assert on a changed value or count.
- Forgetting the negative case. Also assert that records outside the job's scope stay unchanged.
Exercise
Exercise
A test inserts an Account with a null Description, calls a method that enqueues a Queueable job setting Description to 'Processed', and then runs Test.stopTest(). The test asserts Assert.areEqual('Processed', a.Description) using the original a variable, and it fails. What is wrong, and how do you fix it?
Show answer
The variable a still holds the values from before the job ran, because the job updated the database record and not the Apex object. Re-query the record after Test.stopTest(), for example [SELECT Description FROM Account WHERE Id = :a.Id].Description, and assert on that value.
Exercise
Why is it wrong to place an assertion on the job's result between Test.startTest() and Test.stopTest()?
Show answer
The async job is only queued in that window. It runs synchronously when Test.stopTest() is reached, so an assertion placed before that point sees the data as it was before the job ran.