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
Hemanth NeelapuHemanth Neelapu 

Test Class for Apex Schedulable batch procress

Hi Guys,
I need help in writing a test class for the below Apex Schedulable batch Process.
Can anyone help me out?

global class CLM_Batch_ExpireAgreements implements Database.Batchable<sObject>,Schedulable,Database.Stateful {

    //Batch Start method
    global Database.QueryLocator start(Database.BatchableContext BC) {
        string Agreement_Status = 'Activated' ;
        string strQuery='SELECT id,Apttus__Contract_End_Date__c , Apttus__Status__c , Apttus__Auto_Renewal__c ,Apttus__Status_Category__c FROM Apttus__APTS_Agreement__c';    
         strQuery+=' WHERE Apttus__Status__c = \'Activated\' and Apttus__Contract_End_Date__c < today and Apttus__Auto_Renewal__c = False' ;    
            system.debug('  febgdg'+ strQuery);
        return Database.getQueryLocator(strQuery);
    }
    
   //Batch Execute method 
    global void execute(Database.BatchableContext BC, List<Apttus__APTS_Agreement__c> Scope) {
        
        list<Apttus__APTS_Agreement__c> agr = new list<Apttus__APTS_Agreement__c>();
        for(Apttus__APTS_Agreement__c agrObj : scope){
          
           agrObj.Apttus__Status__c ='Expired' ;
           agrObj.Apttus__Status_Category__c ='Expired' ;
            agr.add(agrObj);            
        }
       update agr;
    }
    
     //Batch Finish method    
     global void finish(Database.BatchableContext BC) { 
             
    }
    
   //Method which schedules the batch
       global void execute(SchedulableContext sc) {        
      CLM_Batch_ExpireAgreements snInstance = new CLM_Batch_ExpireAgreements ();
       ID batchprocessid = Database.executeBatch(snInstance);
    } 
    
}
Best Answer chosen by Hemanth Neelapu
Deepali KulshresthaDeepali Kulshrestha
Hi Hemanth,

Try the following test-class, it may be helpful for you:
Batch-Class Test Class:
@isTest 
public class CLM_Batch_ExpireAgreements_Test 
{
    @isTest
    public static void testMethod1() {
        Apttus__APTS_Agreement__c apts=new Apttus__APTS_Agreement__c();
        apts.Apttus__Contract_End_Date__c=System.today() - 5;;
        apts.Apttus__Status__c='Activated';
        apts.Apttus__Auto_Renewal__c=false;
        apts.Apttus__Status_Category__c='Expired';
        Insert apts;
        
        Test.startTest();
            CLM_Batch_ExpireAgreements obj = new CLM_Batch_ExpireAgreements();
            DataBase.executeBatch(obj); 
        Test.stopTest();
    }
}
Scedulable class test-class:
@isTest 
public class scheduleBatch_Test 
{
    @isTest
    public static void testMethod1() {
        Test.startTest();
        scheduleBatch sh1 = new scheduleBatch();
        system.schedule(sh1); 
        Test.stopTest();
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha