4

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
  }
  • 1
    Presuming that currentWeekday == Setting, it should have worked. Are you sure that the values are correct? – sfdcfox May 11 '18 at 22:06
  • @sfdcfox Yes, its working and debug under this if(currentWeekday == Setting) is printing. – Mahwish May 11 '18 at 22:57
1

You won't be able to assert the Id of a queueable job which is the equivalent of what you're trying to do with System.enqueueJob(new SomeClass.acctQueueable()). From the Apex Developer Guide documentation on Queueable Apex:

The ID of a queueable Apex job isn’t returned in test context — System.enqueueJob returns null in a running test.

The only way I can think of to accomplish what you're trying to assert is to use the system limits class with something like getLimitQueueableJobs() both before and after your batch job executes during the test context, then comparing the values that are returned. I'd expect you to see a difference that reflects the job that gets enqueued by the batch class which should reduce the available limit of queueable jobs by 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.