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
Sherwin BetontaSherwin Betonta 

Catch block not covered on Test Class.

Can somebody help me with my code. I created a negative test for Helper Class  to cover the catch block but it's still not successfully covered.

This is my helper class:
public with sharing class AccOppRecalcHelperClass {
    
    //A method which will be called in the trigger
    public static void opportunityTriggerMethod(List<Opportunity> oppList, Map<Id,Opportunity> oldOpp){
        
        //instantiate a list of ID's to store id's of account records
       List<id> accId = new List<id>();
        //instantiate a list of accounts
       List<account> acc = new List<account>();
        //initialize string variable closedWon to Closed Won stage field value stored in custom label
        String Closed_Won = Label.Closed_won;
        //initialize string variable closedLost to Closed Lost stage field value stored in custom label
        String Closed_Lost = Label.Closed_lost;
        String oldaccstat;
        String acctstat;
        Integer oppClosedLost = 0;
        Integer oppClosedWon = 0;
        
        //checks opportunity records from opportunity list
       for(Opportunity opp: oppList){
          //checks if account related to opportunity field is not empty
           if(opp.AccountId != NULL){
               //adds account ID related to opportunity record to list of ID's
               accId.add(opp.AccountId); 
           }
             
       }
        for (Opportunity oppRec : [SELECT Id, Name, StageName FROM Opportunity WHERE AccountId IN :accId AND StageName != NULL]){
            if( oppRec.StageName == Closed_Won){
                oppClosedWon++;
               
            }
            else if (oppRec.StageName == Closed_Lost){
                oppClosedLost++;
            }
        }
        for (Account myAccount : [SELECT ID, Number_of_Opportunities_Won__c, Number_of_Opportunities_Lost__c FROM Account WHERE ID in :accId ]){
            
                myAccount.Number_of_Opportunities_Won__c = oppClosedWon;
                myAccount.Number_of_Opportunities_Lost__c = oppClosedLost;
                acc.add(myAccount);
            
        }
            
        if (acc.size()>0){
            try{
               update acc; 
            }
            catch(Exception e){
                System.debug(e);
            }
        }
        
    } 
}

Here is my negative test method:
@isTest
    private static void negativeTest(){
        List<Account> accts = new List<Account>();
        Account a = new Account(name= 'TestAccount1');
        insert a;
        a.name = 'Sherwin';
        
        Test.startTest();
        accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

		update a;
       	Test.stopTest();
       	System.assert(accts.size() > 0, 'Was expecting to find at least one account');
        System.assertEquals('Sherwin', a.name);
        }

Thanks in advance.
David Zhu 🔥David Zhu 🔥
You won't be able to cover the catch block in your test class. If really needed, you might need to change the code as below:
 
public static void opportunityTriggerMethod(List<Opportunity> oppList, Map<Id,Opportunity> oldOpp){ 
     try
     {
           //put your code here by removing catch block after update acc.
      }
     catch(System.Exception e)
     {}

}

Then in your test class, have a test method with code similar to this:
AccOppRecalcHelperClass.opportunityTriggerMethod(null,null);

 
Sherwin BetontaSherwin Betonta
When I ran the test I get the exception :
System.NullPointerException: Attempt to de-reference a null object
David Zhu 🔥David Zhu 🔥
public with sharing class AccOppRecalcHelperClass {
    
    //A method which will be called in the trigger
    public static void opportunityTriggerMethod(List<Opportunity> oppList, Map<Id,Opportunity> oldOpp){
        try
        {
        //instantiate a list of ID's to store id's of account records
       List<id> accId = new List<id>();
        //instantiate a list of accounts
       List<account> acc = new List<account>();
        //initialize string variable closedWon to Closed Won stage field value stored in custom label
        String Closed_Won = Label.Closed_won;
        //initialize string variable closedLost to Closed Lost stage field value stored in custom label
        String Closed_Lost = Label.Closed_lost;
        String oldaccstat;
        String acctstat;
        Integer oppClosedLost = 0;
        Integer oppClosedWon = 0;
        
        //checks opportunity records from opportunity list
       for(Opportunity opp: oppList){
          //checks if account related to opportunity field is not empty
           if(opp.AccountId != NULL){
               //adds account ID related to opportunity record to list of ID's
               accId.add(opp.AccountId); 
           }
             
       }
        for (Opportunity oppRec : [SELECT Id, Name, StageName FROM Opportunity WHERE AccountId IN :accId AND StageName != NULL]){
            if( oppRec.StageName == Closed_Won){
                oppClosedWon++;
               
            }
            else if (oppRec.StageName == Closed_Lost){
                oppClosedLost++;
            }
        }
        for (Account myAccount : [SELECT ID, Number_of_Opportunities_Won__c, Number_of_Opportunities_Lost__c FROM Account WHERE ID in :accId ]){
            
                myAccount.Number_of_Opportunities_Won__c = oppClosedWon;
                myAccount.Number_of_Opportunities_Lost__c = oppClosedLost;
                acc.add(myAccount);
            
        }
            
        if (acc.size()>0){
               update acc; 
        }
        }
         catch(System.exception e){
           system.debug(e);
            }
        
    } 
}

test method:
@isTest
    private static void negativeTest(){
        try
        {
           AccOppRecalcHelperclass.opportunityTriggerMethod(null,null);
        }
        catch(System.exception e)
        {
         }

  }