Sub topic 12
Testing chained Queueable jobs
This chapter assumes you can write a basic Queueable class and a test that uses Test.startTest and Test.stopTest. It also assumes you know that stopTest runs queued async work synchronously.
What chaining is
A chained job is a Queueable that enqueues another Queueable from its own execute method. Production code often does this to split work into steps, with each step getting its own transaction and governor limits.
Why chaining breaks in a test
Inside a test, stopTest runs the first job. If that job calls System.enqueueJob for a second job, Apex throws System.AsyncException: Maximum stack depth has been reached. A test method can run only one level of a chain. The child job never runs, and the exception fails the test.
Guard the chain, then test each step
The usual fix has two parts:
- Skip the chaining call when
Test.isRunningTest()is true. - Test each job separately, so every step still has coverage.
public class StepOneJob implements Queueable {
private Id accountId;
public StepOneJob(Id accountId) {
this.accountId = accountId;
}
public void execute(QueueableContext context) {
update new Account(Id = accountId, Description = 'Step 1 done');
if (!Test.isRunningTest()) {
System.enqueueJob(new StepTwoJob(accountId));
}
}
}
public class StepTwoJob implements Queueable {
private Id accountId;
public StepTwoJob(Id accountId) {
this.accountId = accountId;
}
public void execute(QueueableContext context) {
update new Account(Id = accountId, AccountNumber = 'Step 2 done');
}
}
@IsTest
private class ChainedJobsTest {
@IsTest
static void stepOneUpdatesDescription() {
Account a = new Account(Name = 'Chain Test');
insert a;
Test.startTest();
System.enqueueJob(new StepOneJob(a.Id));
Test.stopTest();
Account result = [SELECT Description, AccountNumber FROM Account WHERE Id = :a.Id];
Assert.areEqual('Step 1 done', result.Description);
Assert.isNull(result.AccountNumber, 'Step 2 must not run in the same test');
}
@IsTest
static void stepTwoUpdatesAccountNumber() {
Account a = new Account(Name = 'Chain Test 2');
insert a;
Test.startTest();
System.enqueueJob(new StepTwoJob(a.Id));
Test.stopTest();
Account result = [SELECT AccountNumber FROM Account WHERE Id = :a.Id];
Assert.areEqual('Step 2 done', result.AccountNumber);
}
}The first test confirms that step one did its work and that the chain stopped. The second test enqueues step two directly, which is a single-level job and is allowed.
Calling execute directly
A Queueable is an ordinary class, so you can also call execute yourself. This works when the job does not use the QueueableContext argument, so you can pass null:
new StepTwoJob(a.Id).execute(null);This skips the queue entirely. You do not need a test window for it, although a startTest/stopTest pair still gives the code fresh governor limits.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Maximum stack depth has been reached` | The job enqueues a child in a test | Wrap the `enqueueJob` call in `if (!Test.isRunningTest())` |
| Step two has no coverage | The chain stops after step one | Add a separate test that enqueues step two directly |
| The assertion on step two's data fails | Step two never ran in the first test | Assert on step one only, and test step two on its own |
Trade-off of the guard
The Test.isRunningTest() check puts test-only logic in production code, and the line that does the chaining is never executed by a test. Keep that branch as small as one line. Then the untested part is the enqueue call itself, and each job's logic is still fully tested.
Exercise
A test enqueues StepOneJob between startTest and stopTest. StepOneJob enqueues StepTwoJob without a Test.isRunningTest() guard. What happens, and how do you get coverage for StepTwoJob?
Show answer
When stopTest runs StepOneJob, the call to enqueue StepTwoJob throws a "Maximum stack depth has been reached" AsyncException, and the test fails. Guard the chaining call with Test.isRunningTest(). Then write a separate test that enqueues StepTwoJob directly (or calls its execute method) and asserts on its result.