function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marketing Marketing 27Marketing Marketing 27 

Test Class Assertions for Scheduled Batch Class Failing

I have  two classes I'm testing. The first is scheduleBatchJob which implements Schedulable interface. The second is modeSelector which implements Batchable interface.

ModeSelector looks at a bunch of related records for each of the accounts that are processed and updates a text field on each of them.

The debug statements on ModeSelector are showing that it is correctly updating the list of accounts with the correct values but the below test method always shows null for that same updated field every time even when I know it was update with a value. What have I done wrong.k
 
@isTest
    public static void allActiveNoTie(){
        
        Account act = TestDataFactory.getClientAccount('Account');
        TestDataFactory.createBulkDRecords(act.Id, 3, 'Active', 'John M', 500.00);
        TestDataFactory.createBulkLRecords(act.Id, 1, 'Active', 'Bill K', 300.00);
        
        test.startTest();
        
        scheduleBatchJob sj = new scheduleBatchJob();
        String chron = '0 0 13 * * ?';        
        String jobId = system.schedule('Test Sched', chron, sj);        
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
        				  FROM CronTrigger WHERE id = :jobId];
        
        test.stopTest();
        
        system.debug('times fired '+ct.TimesTriggered);
        System.assertEquals(chron, ct.CronExpression);
        Account act2 = [SELECT Id, Name, Fiserv_Resp_Code__c FROM Account 
                                  WHERE Id =: act.Id];
        System.debug('act2 '+act2.Resp_Code__c);
        System.assertEquals(act2.Resp_Code__c, 'John M');

 
Best Answer chosen by Marketing Marketing 27
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
In the test class You are just scheduling the schedular apex. The batch class will run when the schedular some to the time so we cannot expect the values to be updated in that time. 

So you have to remove your last system.assertequals statement.

If you want to check if the batch class is working as expected as not you have to initialise the batch class and run here in the test class and then put this system.assertEquals as below.
 
@isTest
    public static void allActiveNoTie(){
        
        Account act = TestDataFactory.getClientAccount('Account');
        TestDataFactory.createBulkDRecords(act.Id, 3, 'Active', 'John M', 500.00);
        TestDataFactory.createBulkLRecords(act.Id, 1, 'Active', 'Bill K', 300.00);
        
        test.startTest();
        
        scheduleBatchJob sj = new scheduleBatchJob();
        String chron = '0 0 13 * * ?';        
        String jobId = system.schedule('Test Sched', chron, sj);        
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
        				  FROM CronTrigger WHERE id = :jobId];
        
        test.stopTest();
        
        system.debug('times fired '+ct.TimesTriggered);
        System.assertEquals(chron, ct.CronExpression);

modeSelector md= new modeSelector();
database.executebatch(md);       
 Account act2 = [SELECT Id, Name, Fiserv_Resp_Code__c FROM Account 
                                  WHERE Id =: act.Id];
        System.debug('act2 '+act2.Resp_Code__c);
        System.assertEquals(act2.Resp_Code__c, 'John M');

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
In the test class You are just scheduling the schedular apex. The batch class will run when the schedular some to the time so we cannot expect the values to be updated in that time. 

So you have to remove your last system.assertequals statement.

If you want to check if the batch class is working as expected as not you have to initialise the batch class and run here in the test class and then put this system.assertEquals as below.
 
@isTest
    public static void allActiveNoTie(){
        
        Account act = TestDataFactory.getClientAccount('Account');
        TestDataFactory.createBulkDRecords(act.Id, 3, 'Active', 'John M', 500.00);
        TestDataFactory.createBulkLRecords(act.Id, 1, 'Active', 'Bill K', 300.00);
        
        test.startTest();
        
        scheduleBatchJob sj = new scheduleBatchJob();
        String chron = '0 0 13 * * ?';        
        String jobId = system.schedule('Test Sched', chron, sj);        
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
        				  FROM CronTrigger WHERE id = :jobId];
        
        test.stopTest();
        
        system.debug('times fired '+ct.TimesTriggered);
        System.assertEquals(chron, ct.CronExpression);

modeSelector md= new modeSelector();
database.executebatch(md);       
 Account act2 = [SELECT Id, Name, Fiserv_Resp_Code__c FROM Account 
                                  WHERE Id =: act.Id];
        System.debug('act2 '+act2.Resp_Code__c);
        System.assertEquals(act2.Resp_Code__c, 'John M');

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Marketing Marketing 27Marketing Marketing 27
Yes it worked. Thanks so much!

Not sure why, if it's testing the Scheduler class, it does not run the code contained with scheduler execute method.