+ 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) {
            if(objList!= null && objList.size() > 0 ){
                delete objList;
        catch(Exception ex){

My test class:

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

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.

Manjunath S 90Manjunath S 90
Hi Vishal,

Did you get a way to induce an exception in your test class?
Gillette RobertGillette Robert
In your testmethod, insert records in such a way that it will cause the exception to occur. This will cover the exception lines as well. try this in your test class.