You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
Please try below test class
Let us know if this will help you
All Answers
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
Please try below test class
Let us know if this will help you
Getting error while saving
Method does not exist or incorrect signature: void execute() from the type BatchToEndCarouselAnnouncementsScheduler