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
Vishal Tiwari 36Vishal Tiwari 36 

How to Cover Catch block of batch class in your test class?

I have a class something like this:

global void execute(Database.BatchableContext BC, List<OBJ__c> objList) {
        try{
            if(objList!= null && objList.size() > 0 ){
                delete objList;
            }
        }
        catch(Exception ex){
            System.debug('Exception');
        }
    }

My test class:

 List<OBJ__C> cartList = [SELECT id from OBJ__C LIMIT 1]; 
        Test.startTest();
        BATCH_CLASS deleteCartBatch = new BATCH_CLASS ();
        Database.executeBatch(deleteCartBatch, 200); 
        Test.stopTest();


How do I cover the catch block in this test class?

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Vishal,

To cover the catch block as well I think you should make sure the implementation in test class causes an exception there by that exception can be caught and in turn cover the catch block.

I hope this helps.

Regards,
Anutej 
Manjunath S 90Manjunath S 90
Hi Vishal,

Did you get a way to induce an exception in your test class?
Fred LarsonFred Larson
I think you should make sure the implementation in test class causes an exception there by that exception can be caught.

advancedmd.com (https://www.advancedmd.review/)