2

I've got a requirement to perform queueable apex in a trigger my class working fine, but while writing a test class for queueable apex I'm getting an exception. Could anybody can help me.

Trigger example on opportunity(After update){
if(Trigger.isAfter && Trigger.isUpdate){
        System.enqueuejob(new  Oppty_TargetHandlerContainer(Trigger.old));
}
if(Trigger.isAfter && trigger.isUpdate){
           System.enqueuejob(new  Oppty_TargetHandlerContainer(Trigger.new));
 }

}

This my test class

@isTest
public class Oppty_TargetHandlerContainerTest {
//varibale Declaration

static void init(){
    //Initializing the data
    }

    //}
}

//Method to cover opportunity handler.
static testMethod void opportunityTriggerMethod1(){
  init();
  Test.startTest();
   System.enqueuejob(new  Oppty_TargetslogicHandler(opplist));
  Test.stopTest();
  Test.startTest();
   System.enqueuejob(new  Oppty_TargetHandlerContainer(opplist));
  Test.stopTest();
}  
}

The following are the my queueable classes

public class Oppty_TargetHandlerContainer implements Queueable {
@Testvisible
private List<Opportunity> optylist;
public Oppty_TargetHandlerContainer(List<Opportunity> optys) {
    this.optylist = optys;
}
public void execute(QueueableContext qc) {
    System.enqueueJob(new Oppty_TargetslogicHandler(optylist));
}
}

Logic queueable class

public class Oppty_TargetslogicHandler implements Queueable {
//Variable declaration
public Oppty_TargetslogicHandler(List<Opportunity> optys) {
    //storing data in variables
}
public void execute(QueueableContext qc) {
    updating some other object,
    there is a recursive trigger

}
}
2
  • I tried solutions what you guys are suggested me, Could you help to resolve this problem?, Thanks in advance.
    – Gangadhar
    Apr 4, 2019 at 6:21
  • I edited my answer below - did the two separate methods work? Apr 4, 2019 at 12:23

3 Answers 3

2

In your Oppty_TargetHandlerContainerTest:: opportunityTriggerMethod1() test method, you are running two asynchronous jobs, which is not allowed in a single test method. Only one job can be run per test method.

Put each job into a separate test method function and it should work.

EDIT

The code should look something like this:

static testMethod void opportunityTriggerMethod1(){
   init();

   Test.startTest();
   System.enqueuejob(new  Oppty_TargetslogicHandler(opplist));
   Test.stopTest();
}

static testMethod void opportunityTriggerMethod2(){
   init();

   Test.startTest();
   System.enqueuejob(new  Oppty_TargetHandlerContainer(opplist));
   Test.stopTest();

    // Optional Assert methods to confirm functionality
}  
2
  • Thanks for your response @Brian Miller, I added Following method to the my code still i'm facing same problem, static testMethod void opportunityTriggerMethod2(){ opportunityTriggerMethod1(); Test.startTest(); System.enqueuejob(new Oppty_TargetHandlerContainer(opplist)); Test.stopTest(); }
    – Gangadhar
    Apr 4, 2019 at 5:34
  • Don't call opportunityTriggerMethod1() within that testMethod. Have two completely separate test methods. See the updated answer Apr 4, 2019 at 6:43
1

I think it's because you can't chain queueable jobs while testing in Apex. Please refer to this System.AsyncException: Maximum stack depth has been reached

1
  • Tyhanks for your response @Vincenzo Laudato, I tried it, I got exception like this Constructor not defined: [Oppty_TargetslogicHandler].<Constructor>().
    – Gangadhar
    Apr 4, 2019 at 5:42
-2
@isTest
public class Oppty_TargetHandlerContainerTest {
//varibale Declaration

static void init(){
//Initializing the data
}

//}
}

//Method to cover opportunity handler.
static testMethod void opportunityTriggerMethod1(){
  init();
  Test.startTest();
  Oppty_TargetslogicHandler op=new Oppty_TargetslogicHandler(opplist);
  //opplist is nothing but list of optys
  op.execute(null);
  Test.stopTest();
}   

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .