+ Start a Discussion
Nas UserNas User 

Flex queue in testschedule

Hi,
     I have a scheduled class which calls almost 15 batch jobs. I have manually invoked these batch jobs and flex queue is taking care of the 5 concurrent jobs limit. But when the scheduled class is invoked in a test class it throws an error showing that more than 5 jobs cannot be queued. Do I have to specify jobs in flex queue in test classes? I cannot find any such thing in documentation 
Best Answer chosen by Nas User
Suraj GharatSuraj Gharat
Salesforce allows maximum 5 batch jobs submission in test context. See section "Force.com Platform Apex Limits" in below link,

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

To avoid this test failure, you may use "isRunningTest()" method from "Test" class to detect the execution context, for instance:
 
Database.executeBatch(b1);
Database.executeBatch(b2);
Database.executeBatch(b3);
Database.executeBatch(b4);
Database.executeBatch(b5);
if(!Test.isRunningTest()){
	Database.executeBatch(b6);
}

 

All Answers

Suraj GharatSuraj Gharat
Salesforce allows maximum 5 batch jobs submission in test context. See section "Force.com Platform Apex Limits" in below link,

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

To avoid this test failure, you may use "isRunningTest()" method from "Test" class to detect the execution context, for instance:
 
Database.executeBatch(b1);
Database.executeBatch(b2);
Database.executeBatch(b3);
Database.executeBatch(b4);
Database.executeBatch(b5);
if(!Test.isRunningTest()){
	Database.executeBatch(b6);
}

 
This was selected as the best answer
Nas UserNas User
Hi Suraj,
            Thanks for clearing that up. I thought the latest flex queue would take care of this for me.