writing test class for Scheduler and Batches

How to write a test class for Scheduler and Batches:

Here is the example to Write test method for Scheduler and Batch Apex Classes:

Example: This batch Apex is to Approving the opportunities which are in submitted status.

// Scheduler
global class OpportunityScheduler implements Schedulable{
global void execute(SchedulableContext sc){
OpportunityBatch batch = new OpportunityBatch();

// Batch
global class OpportunityBatch implements database.batchable<sObject>{
global String opptyList;
global Database.QueryLocator start(Database.BatchableContext info){
String status = ‘Submitted’;
opptyList = ‘select name,AccountName__c from Opportunity where
status__c =\”+ status +’\” ;
return Database.getQueryLocator(claimList);
global void execute(Database.batchableContext info,List<Opportunity> opptyList){
List<Opportunity> opportunitiesList = new List< Opportunity >();
for(Opportunity oppty: opptyList){
oppty.status__c = ‘Approved’;
Insert opportunitiesList;
global void finish(Database.batchableContext info){


// Scheduler test method
class OpportunitySchedulerTest{
public static testMethod void testschedule() {
OpportunityScheduler testsche = new OpportunityScheduler();
String sch = ‘0 0 23 * * ?’;
system.schedule(‘Test status Check’, sch, testsche );

// Batch test method
public class OpportunityBatchtest{
//query the activities to process
static testmethod void OpportunityTestMethod(){
Opporunity oppty = new Opportunity ();
oppty.name = ‘test Oppty’;
oppty.status__c = ‘submitted’;
insert oppty;
OpportunityBatch opptybatch = new OpportunityBatch ();
Status = ‘Submitted’;
opptybatch.opptyList = ‘select name,AccountName__c from Opportunity where status__c =\”+ status +’\’ and id=\’ AND Id=\”+claim.id+’\”;

Leave a Reply