Sub topic 7
Testing Queueable jobs
This chapter assumes you can write a basic @IsTest class, use SObject DML, and know what a Queueable class does. It builds on the test window from the previous chapters: Test.startTest and Test.stopTest.
Why the test window matters for Queueable
System.enqueueJob does not run your job right away. The platform puts it in a queue and runs it later, in its own transaction. A test that enqueues a job and then asserts immediately would read the data from before the job ran.
Inside a test, the rules change. Any job enqueued between Test.startTest() and Test.stopTest() runs synchronously when stopTest is reached. After stopTest returns, the job has finished and its changes are in the database.
A complete example
The job below takes an Account Id and sets its description. It keeps only the Id, not the whole record, so the job gets the current data when it runs.
public class DescriptionJob implements Queueable {
private Id accountId;
public DescriptionJob(Id accountId) {
this.accountId = accountId;
}
public void execute(QueueableContext context) {
update new Account(Id = accountId, Description = 'Processed');
}
}
@IsTest
private class DescriptionJobTest {
@IsTest
static void jobUpdatesAccount() {
Account a = new Account(Name = 'Queue Test');
insert a;
Test.startTest();
Id jobId = System.enqueueJob(new DescriptionJob(a.Id));
Test.stopTest();
Assert.isNotNull(jobId);
Assert.areEqual('Processed', [SELECT Description FROM Account WHERE Id = :a.Id].Description);
}
}Reading the test step by step
- Setup comes first. The Account is inserted before
startTest. This data is part of the test's own setup and does not count against the limits of the code under test. - Enqueue inside the window.
System.enqueueJobreturns a job Id. The job is now waiting to run. stopTestruns the job. Execution blocks untilexecutefinishes.- Assert after the window. The test queries the Account again and checks the new value. The
avariable in memory still holds the old data, so readinga.Descriptionwould fail.
The returned job Id is useful as a basic check that the enqueue worked. The real proof is the changed record.
Common mistakes
- Asserting before
stopTest. The job has not run yet, so the old values are still in the database. - Checking the in-memory record. Always re-query with SOQL after
stopTest. - Enqueuing outside the window. A job enqueued before
startTestor afterstopTestdoes not get the synchronous run. KeepSystem.enqueueJobbetween the two calls. - Calling
startTesttwice. A test method can use only onestartTestandstopTestpair.
Jobs that make callouts
If the job calls an external service, the test must register a mock with Test.setMock before startTest. Real HTTP callouts are not allowed in tests. A later chapter covers mocks in detail.
Exercise
A test inserts an Account, calls System.enqueueJob(new DescriptionJob(a.Id)) before Test.startTest(), then calls Test.startTest() and Test.stopTest() with nothing between them, and asserts that the description is 'Processed'. Why can the assertion fail, and how do you fix it?
Show answer
The job is enqueued outside the test window, so it is not forced to run synchronously at stopTest, and the description may not be updated when the assertion runs. Move System.enqueueJob between Test.startTest() and Test.stopTest(), then re-query the Account and assert.
Exercise
After Test.stopTest(), a test asserts on a.Description, where a is the Account variable created earlier in the test. The job did run. Why does the assertion still fail?
Show answer
The variable a holds the data from before the job ran. The job changed the record in the database, not the object in memory. Re-query the record with SOQL after stopTest and assert on the queried result.