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
Guru 91Guru 91 

Help me with test class for batch apex?

Hi,
Can you please help me with test class


Class:

global with sharing class BatchToEndCarouselAnnouncementsScheduler extends BatchScheduler implements Schedulable
{
    global BatchToEndCarouselAnnouncementsScheduler()
    {
        super(10 /*minutes between attempts */, 'End Carousel Announcements');
    }
    
    global void execute(SchedulableContext sc)
    {
        schedule(new BatchToEndCarouselAnnouncements());
    }
}


Extended Class:

public with sharing abstract class BatchScheduler implements Schedulable
{
    @TestVisible static final Integer MAX_CONCURRENT_BATCH = 5;
    @TestVisible Integer minutesBeforeReschedule;
    @TestVisible String title;
    public BatchScheduler( Integer minutes, String title )
    {
        this.minutesBeforeReschedule = minutes;
        this.title = title;
    } 
    public void schedule( IBatchHelper helper, Integer batchSize )
    {
        Integer currentlyRunningBatches = [SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex'
                                           AND (Status = 'Processing' OR Status = 'Preparing')];
        if( currentlyRunningBatches < MAX_CONCURRENT_BATCH )
        {
            BatchHandler batch = new BatchHandler( helper );
            Database.executeBatch( batch, batchSize );
        }
        else
        {
            scheduleRetry();
        }
    }

    public void schedule( IBatchHelper helper )
    {
        schedule( helper, 200 );
    }

    @TestVisible void scheduleRetry() 
    { 
        Datetime rescheduleDateTime = Datetime.now().addMinutes(minutesBeforeReschedule); 
        String timeForScheduler = rescheduleDateTime.format('s m H d M \'?\' yyyy'); 
        System.schedule(title + ' ' + timeForScheduler, timeForScheduler, this); 
    } 
}
Best Answer chosen by Guru 91
Amit Chaudhary 8Amit Chaudhary 8

Please try below test class
@isTest
private class BatchToEndCarouselAnnouncementsSchTest
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;
        
        Test.startTest();
			
			BatchToEndCarouselAnnouncementsScheduler  obj = new BatchToEndCarouselAnnouncementsScheduler();
			obj.execute();

        Test.stopTest();
        // Add assert here to validate result
    }
}

Let us know if this will help you

 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Guru,

Please follow this post, it will be very helpful for you to write test class for your schedulable class:
http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

Just need to pass the new instance of your this class "BatchToEndCarouselAnnouncementsScheduler" to in System.schedule() inside test method given in the above link post

Let me know if you face any issue.
Thanks
Niraj
Amit Chaudhary 8Amit Chaudhary 8

Please try below test class
@isTest
private class BatchToEndCarouselAnnouncementsSchTest
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;
        
        Test.startTest();
			
			BatchToEndCarouselAnnouncementsScheduler  obj = new BatchToEndCarouselAnnouncementsScheduler();
			obj.execute();

        Test.stopTest();
        // Add assert here to validate result
    }
}

Let us know if this will help you

 
This was selected as the best answer
Guru 91Guru 91
Hi Amit,
Getting error while saving 

Method does not exist or incorrect signature: void execute() from the type BatchToEndCarouselAnnouncementsScheduler
Guru 91Guru 91
Thanks Amit,