I have 2 queueable classes and i have chained them in my account update trigger
the logic is like below
Trigger Logic- onAccountUpdate
{
Queueableclass1 que1 = new Queueableclass1(newList);
System.enqueue(que1);
}
Queueable class 1
Execute()
{
Some Logic…
Queueableclass2 que2 = new Queueableclass2(newList);
System.enqueue(que2);
}
in Testclass
I have created the Account test data and made an update to test this scenario. But the it is throwing maximum depth reached error as it is trying to execute both jobs in same test method.
So I understand that I cannot run 2 async jobs from one method. Can Somebody please suggest me that how can I fix my trigger test class..?
Best Answer
So, you can't test chained queueables at depth more than 1 in a testmethod
There are multiple parts to the answer
Gate all your
System.enqueue
with a reference to a Utility propertye.g.
you can dependency inject the value of
Util.isEnqueueable = false
to control in testmethods whether to enqueue a queueable jobBreak your problem into two unit tests:
For the first unit test
set
Util.isEnqueueable
= false;DML the record in the testmethod
assert that there is an
AsyncApexJob
for the queueable class 1For the second unit test
Construct an object of the first queueable
Then invoke its
execute()
methodAssert that the second queueable (called by the first) does what it is supposed to do)