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
Shubhangi JadhavShubhangi Jadhav 

Can anyone help me to cover test class for Try and catch

Please help me to get this lines coverd in my test class:



       try{
           if(errorFlagfromIP != null && errorFlagfromIP instanceOf Boolean)
             {
               Boolean failFlag =(Boolean)errorFlagfromIP;
               
               if(failFlag==true) // Error Code Received from the Interface (SOA ERROR)
                        {
                        
                         Boolean errorFromInterface=true;  
                                              
                       if(errorFromInterface)
                       
                               {
                               System.debug('Callout has error from interface : ' + errorFromInterface);
                               }              
                       
                                                }
                        if(failFlag==false) 
                         {
                         
                         throw new vlocity_cmt.XOMOrchestrationInterfaces.SystemInterfaceRecoverableException('No Response Returned From HTTP Action or Interface');
                         
                         }
 

           }
       }Catch(CalloutException e)
           {
            throw new vlocity_cmt.XOMOrchestrationInterfaces.SystemInterfaceRecoverableException ('Callout exception ' + e + ': trying to retry');    
           }
SwethaSwetha (Salesforce Developers) 
HI Shubhangi,

Since this code provided is not complete and requires an understanding of your implementation, it might not be possible to provide exact test class suggestions. However, the below information should help you get started.

To get the catch block to execute, an exception would need to originate from the try block of code in the trigger. If your trigger was doing an update, some validation elsewhere might result in a DMLException being thrown.

The important thing about your test methods is that they verify your code works correctly. Unless you have a way of crafting a record that will fail (1st suggestion above), the only other method-- which is strongly not recommended-- would be to write an exception built into the actual code you're testing. This involves using Test.isRunningTest to set up an arbitrary failure.

Test Code coverage catch block
Please note that Not all code that is written can be covered. This is a fact of salesforce.com development. There is a reason why the deployment rule is 75% coverage, and not 100% coverage.

References: https://salesforce.stackexchange.com/questions/61558/how-to-cover-try-catch-in-test-classes
https://salesforce.stackexchange.com/questions/16727/test-code-coverage-catch-block

​​​​​​​Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you