As Salesforce document You can’t chain queueable jobs in an Apex test. Doing so results in an error. To avoid getting an error, you can check if Apex is running in test context by calling Test.isRunningTest() before chaining jobs.
You can refer below link
https://salesforce.stackexchange.com/questions/181233/system-asyncexception-maximum-stack-depth-has-been-reached
Now in your code change List l/List LA to List<Account> l, List<Account> LA. There are 2 cases in which you will get error.
1. Recursive trigger error i.e Too many queueable jobs added to the queue: 2
2. For chaining the Queueable Job in Test class: System.AsyncException: Maximum stack depth has been reached.
Please Use below code to resolve your issue
Parent Queueable
public with sharing class Qapex implements Queueable{
private list<Account> L;
public Qapex(List<Account> a) {
this.L= a;
}
public void execute(QueueableContext q){
list<Account> LA= new list<Account>();
for(account f: this.L){
account acc= new account();
acc.Name= ‘queable name’ + f.Name;
LA.add(acc);
}
INSERT LA;
if(!Test.isRunningTest()){
system.enqueueJob(new Qapex1());
}
}
}
Child Queueable
public with sharing class Qapex1 implements Queueable{
public void execute(QueueableContext QC){
account ac = new account(name= ‘new name’);
insert ac;
}
}
Trigger
if(Trigger.isBefore)
{
if(Trigger.isInsert || Trigger.isUpdate)
{
if(RecursiveTriggerHandler.isFirstTimeBeforeIns){
system.enqueueJob(new Qapex(trigger.new));
RecursiveTriggerHandler.isFirstTimeBeforeIns=false;
}
}
}
Test Classe for Qapex
@istest
public class QapexTCls {
@istest
public static void Qapex1TClsM() {
account a= new account(name= ‘qapextestname’);
test.startTest();
insert a; //this will call trigger that is used to invoke queueable class
test.stopTest();
account ac= [select id, name from account where id= : a.id];
system.assertEquals(‘qapextestname’, a.name);
}
}
Test Class for Qapex1
@istest
public class Qapex1TCls {
@istest
public static void Qapex1TClsM() {
test.startTest();
system.enqueueJob(new Qapex1());
test.stopTest();
}
}