+ Start a Discussion
Inbox OutboxInbox Outbox 

Test class for chained queueable apex

System.AsyncException: Maximum stack depth has been reached.
Class.Qapex.execute: line 14, column 1
I am new to salesforce. I was pulling my hair trying to figure out this queueable apex and I want to know how to write a test class for the below code. I have chained 1 child job. 
Please give examples


Parent queueable class:
public with sharing class Qapex implements Queueable{
    private list L;
        public Qapex(List a) {
            this.L= a;
        }
    public void execute(QueueableContext q){
        list LA= new list();
        for(account f: this.L){
            account acc= new account();
            acc.Name= 'queable name' + f.Name;
            LA.add(acc);
        }
        INSERT LA;
        system.enqueueJob(new Qapex1());
    }
    }

Child queueable class: 
public with sharing class Qapex1 implements Queueable{
    public void execute(QueueableContext QC){
        account ac = new account(name= 'new name');
        insert ac;
    }
   
}

Trigger:
trigger QapexTgr on Account (before insert) {
 if(trigger.isBefore){
     if(trigger.isInsert){
         system.enqueueJob(new Qapex(trigger.new));
     }
 }
}

Parent queuable test class:
@istest
public with sharing class QapexTCLS {
    public testmethod static void QapexTCLSM(){
            Account a= new Account();
            a.Name= 'test account';
        
        insert a;
        test.startTest();
        system.enqueueJob(new Qapex(trigger.new));
        test.stopTest();

        account acc= [SELECT ID, name FROM account WHERE ID = : A.ID];
        system.assertEquals('test account', a.name);
    }

}

Child queueable test class:
@istest
public class Qapex1TCls {
   @istest
    public static void Qapex1TClsM() {
    account a= new account(name= 'qapextestname');
    insert a;

    test.startTest();
    system.enqueueJob(new Qapex1());
    test.stopTest();

    account ac= [select id, name from account where id= : a.id];
    system.assertEquals('qapextestname', a.name);
    }
}

ERROR:
QapexTCLS.QapexTCLSM  Fail     System.AsyncException: Maximum stack depth has been reached.  
                               Class.Qapex.execute: line 14, column 1   

This class name's value is invalid: Qapex1TCls. Provide the name of an Apex class that has test methods.
Best Answer chosen by Inbox Outbox
Yogendra JangidYogendra Jangid
Hi,
As per salesforce documentation of chaining of APEX Queueables 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

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.
So you need to skip the chained queueable and create a standalone test class for both Queueables.
public with sharing class Qapex implements Queueable{
    private list L;
        public Qapex(List a) {
            this.L= a;
        }
    public void execute(QueueableContext q){
        list LA= new list();
        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());
    }
    }

hope this answers your question, if so please can you mark this as best answer. Thanks
 

All Answers

Yogendra JangidYogendra Jangid
Hi,
As per salesforce documentation of chaining of APEX Queueables 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

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.
So you need to skip the chained queueable and create a standalone test class for both Queueables.
public with sharing class Qapex implements Queueable{
    private list L;
        public Qapex(List a) {
            this.L= a;
        }
    public void execute(QueueableContext q){
        list LA= new list();
        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());
    }
    }

hope this answers your question, if so please can you mark this as best answer. Thanks
 
This was selected as the best answer
Inbox OutboxInbox Outbox
Yogendra Jangid, 
Thank you for the reply. I didn't know how to add TEst.isRunningTest().

I added the test.isRunningTest() to the queued job but I am getting this error. I changed nothing but adding that. 

 System.NullPointerException: Attempt to de-reference a null object  
                               Class.Qapex.execute: line 8, column 1