The queueable job is enqueued in the finish method of the batch class but I am unable to assert in the test that queueable job got enqueued.
System.enqueueJob was returning null Id in the test then I tried to set a static boolean variable to true before System.enqueueJob line but the boolean variable is returning false in the test class.
Batch Class:
@TestVisible static Boolean queueableIsQueued = false;
global void finish(Database.BatchableContext bc){
if(currentWeekday == Setting) {
queueableIsQueued = true; // setting here to true
System.debug('@BatchVar:: '+queueableIsQueued); // returns true
System.enqueueJob(new SomeClass.acctQueueable());
}
}
Test Class:
Test.startTest();
Database.executeBatch(batch);
Test.stopTest();
System.debug('@testVar::'+BatchClassName.queueableIsQueued); // return false
//This line is executes before the debug in the batch finished method
EDIT: Additional Details
Following are the options I tried to do a assert in test class. Seems like the finish method simply isn’t being called until after the (debug in test class) /assertion
Option 1:
public static Id jobId;
Batch Class:
{
global void finish(Database.BatchableContext bc){
if(currentWeekday == Setting) {
jobId = System.enqueueJob(new SomeClass.acctQueueable());
}
System.debug('jobId: '+jobId);
}
}
Debug:
{
jobId: null
}
Option 2:
Test Class:
{
Test.startTest();
Database.executeBatch(batch);
Test.stopTest();
System.debug('list: '+[SELECT Id, JobType, Status, CompletedDate, MethodName FROM AsyncApexJob WHERE JobType = 'Queueable']);
}
Debug:
{
list: () // List was empty
}
Option 3:
Batch Class:
{
@TestVisible static Boolean queueableIsQueued = false;
global void finish(Database.BatchableContext bc){
if(currentWeekday == Setting) {
System.enqueueJob(new SomeClass.acctQueueable());
}
List<AsyncApexJob> lst = [SELECT Id, JobType, Status, CompletedDate, MethodName FROM AsyncApexJob WHERE JobType = 'Queueable'];
if(lst.size() > 0) {
acctMetricsQueueableIsQueued = true;
System.debug('list: '+lst);
}
}
}
Test Class:
{
Test.startTest();
Database.executeBatch(batch);
Test.stopTest();
System.debug('var: '+BatchClass.queueableIsQueued);
}
Debug:
{
Var: false
list: (AsyncApexJob:{Id=7072900000XaKfBAAV,...}
}
Option 4:
Queueable Class:
{
public static Boolean queueableIsQueued = false;
// queueable class constructor
acctQueueable() {
queueableIsQueued = true;
System.debug('Queueable class var:'+queueableIsQueued);
}
}
Test Class:
{
Test.startTest();
Database.executeBatch(batch);
Test.stopTest();
System.debug('var: '+QueueableClass.queueableIsQueued);
}
Debug:
{
Var: false
Queueable class var: true
}
Option 5:
Batch Class:
{
@TestVisible static Boolean queueableIsQueued = false;
global void finish(Database.BatchableContext bc){
if(currentWeekday == Setting) {
queueableIsQueued = true; // setting here to true
System.debug('@BatchVar: '+queueableIsQueued); // returns true
System.enqueueJob(new SomeClass.acctQueueable());
}
}
}
Test Class:
{
Test.startTest();
Database.executeBatch(batch);
Test.stopTest();
System.debug('var: '+BatchClass.queueableIsQueued);
}
Debug:
{
var: false
@BatchVar: true
}