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
RuniRuni 

code coverage test class

How to write test class for below class.

public with sharing class RecordsBatch implements Database.Batchable<sObject>,Schedulable{
    public Database.QueryLocator start(Database.BatchableContext pBatchContext) {
        system.debug('\n\n Label.RecordsBatchQueryOnFeeSplit :: '+Label.RecordsBatchQueryOnFeeSplit);
        return Database.getQueryLocator(Label.RecordsBatchQueryOnFeeSplit);
    }
    public void execute(Database.BatchableContext pBatchContext, List<bank__C> listFeeSplit) { 
        system.debug('...listFeeSplit...'+listFeeSplit);        
        List<bank__C> UpdateFeeSplit =  new List<bank__C>();                
        for(bank__C oppFS: listFeeSplit){
            bank__C UpdateObjFS = new bank__C();           
                system.debug('WIP*****');          
            if(oppFS.Recipient__r.Service_Line__c != oppFS.Recipient_Service_Line__c
               || oppFS.Recipient__r.Business_Unit__c != oppFS.Recipient_Business_Unit__c
               || oppFS.Recipient__r.Revenue_Generating_Team__c != oppFS.Recipient_Revenue_generating_team__c;
               || oppFS.Recipient__r.RGT_ID__c != oppFS.Recipient_RGT_ID__c){
                    
                    UpdateObjFS.id = oppFS.ID;
                    UpdateObjFS.Recipient_Service_Line__c = oppFS.Recipient__r.Service_Line__c;
                    UpdateObjFS.Recipient_Business_Unit__c = oppFS.Recipient__r.Business_Unit__c;
                    UpdateObjFS.Recipient_Revenue_generating_team__c = oppFS.Recipient__r.Revenue_Generating_Team__c;
                    UpdateObjFS.Recipient_RGT_ID__c = oppFS.Recipient__r.RGT_ID__c;
                    
                    system.debug('WIP UpdateObjFS*****'+UpdateObjFS);
                    UpdateFeeSplit.add(UpdateObjFS);              
               }                        
        }
         if(!UpdateFeeSplit.isEmpty()){
            Database.update(UpdateFeeSplit,false);
        }  
    }
     public void execute(SchedulableContext ctx) {
        RecordsBatch oWB = new RecordsBatch();
        database.executebatch(oWB,Integer.valueof(Label.RecordsBatchLimit));
    }
    public void finish(Database.BatchableContext bc){
    }
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Runi,

Refer the below link will help you to create test class for batch class.

https://jayakrishnasfdc.wordpress.com/2021/01/02/apex-test-class-for-batch-apex/

Let me know if any issues, will help you with code.

If this helps, Please mark it as best answer.

Thanks!!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Runi,

try with below code sample and keep the mandatory data.
 
@isTest
private class RecordsBatchTest {
    static testMethod void testme(){
		//insert mandory fields
		Recipient__c rec = new Recipient__c();
		rec.Name = 'test';
		rec.Amount__c =2000;
		insert rec;
		
	    //insert mandory fields
        Bank__c Ban = new Bank__c();
        Ban.Name='SBI';
        Ban.Code__c='1234';
        Ban.Recipient__c =rec.id;
		Ban.Recipient_Service_Line__c='test line';
        insert Ban;
		
        Test.startTest();
        RecordsBatch ba= new RecordsBatch();
        Id jobid= Database.executeBatch(ba,5);
        Test.stopTest();


    }
}

If this helps, Please mark it as best answner.

Thanks!!