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
Sid LightningSid Lightning 

Need Help in writting Test Class for Batch Apex that deletes records?

Hi, 

I need help in writting test class for this batch class, It deletes opportunity if the custom record(child to Opportunity) is not present.

Opportunity is child to Account.

Please find the codee

global class OpportunityDeleteBatch implements Database.Batchable<sObject>,Schedulable{
    
    global void execute(SchedulableContext sc){
        OpportunityDeleteBatch bc = new OpportunityDeleteBatch();
        database.executeBatch(bc);
    }
    global Database.QueryLocator start(Database.BatchableContext bc){
        return database.getQueryLocator([Select Id, Name From Opportunity WHERE RecordType.Name= 'Retail - FR' AND ID NOT IN ( Select Opportunity__c from Product_Interest__c)]);
    }
    global void execute(Database.BatchableContext bc,List<Opportunity> opp){
        if(!opp.isEmpty()){
            database.delete(opp,false);
        }
    }
    global void finish(Database.BatchableContext bc){
        
    }

}



 
Lokesh KumarLokesh Kumar
@isTest
Public class OpportunityDeleteBatchTest{

static testmethod void validateBatchclass(
Opportunity opp = new Opportunity(Name=‘Test’, Closedate=System.Today(),Stage=‘Prospect’, RecordTypeId = ‘**************’);
insert opp;
Test.startTest();
Database.executeBatch(new OpportunityDeleteBatch(), 10);
Test.stopTest();

)
}

Update your RecordTypeID for Retail - FR in the above code and this will cover your Batch class 100%.