+ Start a Discussion
Manohar kumarManohar kumar 

CronTrigger not firing in test class !!

Hi Everyone,

i have a batch class which insets Results__c records. i Wrote a test class for that, which is working fine and also able to debug inserted record id.

but when i am putting assert(insertedResultRecls.size()>0), its falling and only covers some line on from the top.

when i remove assert statement and try to qury  Results__c records, its showing empty. but in debug its creating record.

And when i try to see if CronTrigger is fired or not, it was not fired. 

batch1 obj = new batch1();
        Test.startTest();
        Id batchJobId = DataBase.executeBatch(obj); 
        List<Result__c> assls = new List<Result__c>();
        system.debug('assls.size'+assls.size());  // coming 0
        //system.assert(assls.size()>0);
        Test.stopTest();
        system.debug('batchJobId :'+batchJobId);
        List<CronTrigger> cronTrigger  = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :batchJobId];  // not returning any value
        //System.assertEquals(cronTrigger.TimesTriggered, 1);
        system.debug('cronTrigger :'+cronTrigger[0]);
        system.debug('cronTrigger.TimesTriggered :'+cronTrigger[0].TimesTriggered);


Any help would be appreciated.

Thanks

Manohar 

Lokesh KumarLokesh Kumar
Hi Manohar,

As i am seeing you are not inserting any record for Result__c. Please do first and then check for more information please visit this link.

https://developer.salesforce.com/forums/?id=906F0000000kD8eIAE

Thanks
Lokesh
Manohar kumarManohar kumar

Hi Lokesh,

Thanks for your reply. i am inserting Result__c through the batch, which i want to cover.

i inserted all the records which batch needs to cover. Any reason why cronntrigger is not getting fired ?

Thnaks

Manohar

 

Lokesh KumarLokesh Kumar
Hi Manohar,

Are you getting BatchJob ID or Not to validate please put in System.debug logs and let me know.

Thanks
Lokesh

 
Manohar kumarManohar kumar

yes, i am gettting job id. but after that i am not getting anything. 

system.debug('batchJobId :'+batchJobId);
        List<CronTrigger> cronTrigger  = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :batchJobId];  // not returning any value
        //System.assertEquals(cronTrigger.TimesTriggered, 1);
        system.debug('cronTrigger :'+cronTrigger[0]);

but 
        system.debug('cronTrigger :'+cronTrigger[0]); its coming null, query is no recturning anything with this jobid .

Thnaks,

Lokesh KumarLokesh Kumar
Hi Manohar,

Why you are querying CronTrigger as you are executing a Batch Apex so please query AsyncApex Objects there you will get your job id and able to assert correctly.
 
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
      TotalJobItems, CreatedBy.Email
      FROM AsyncApexJob WHERE Id =
      :batchJobId];


Thanks
Lokesh