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
nbnb 

code coverage for catch block

Hi

 

I have a requirement where i have to pass the exception as parameter to a method in another class in the catch block. I tried creating an exception by inserting an empty list in the try block but nothing happened.
Can you tell me if there is any other way to obtain code coverage for it?

Thanks 
NB

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You won't be able to get coverage on your catch block in the scheduler, as you are creating the exception in the test class and passing it to the exception handler.

 

You'll actually need to generate the exception in the scheduler class.  If you can set up data that will cause an exception then you will get the coverage.  If not, you could use dependency injection to set a flag on the scheduler, and when that flag is set cause an exception by dereferencing a null pointer or similar.  My personal opinion is that is the wrong way to go, as you are adding code to production to allow tests to run.

All Answers

bob_buzzardbob_buzzard

This is one of those areas where the platform isn't helping you, in that you can't manufacture error situations because the bad data etc is trapped too early.  What you want to avoid is writing code that creates an exception, as that will never happen in the real world.

 

Can you post the code - otherwise its difficult to tell if there is a data setup that will cause the error.

nbnb

Hi 

 

my code will look lik this:

 

try{

 

//calling a class  A

 

}

catch (exception e){

 

string process =' Sample';

 

//calling another class B.method 1(e,obj)

}

 

Class B.

public class b{

 

}

 

 

 

 

I want to write code coverage for the catch block in the above code.

 

Thanks

NB

nbnb
I am also using exception e.getMessage() methods in the class B
nbnb

Hi

 

Here is the actual code:

It is a scheduler class..

global void execute(SchedulableContext SC)     
    {
        
        try{
           //some code

          calling scheduler
        }
        catch(exception e){
            //eventlog is another class
            eventlog.createrecord(e);


           calling scheduler again
                }
        }
  

In the test class i tried the following:

try{
                    List<Case> caselist = new List<Case>();
                    update caselist[1];
                      }
                    catch(exception e){
                    eventlog.createrecord(e);
                    //rest of the code
                    }

 

But i could not obtain the code coverage...

 

Thanks,

Nagavi

bob_buzzardbob_buzzard

You won't be able to get coverage on your catch block in the scheduler, as you are creating the exception in the test class and passing it to the exception handler.

 

You'll actually need to generate the exception in the scheduler class.  If you can set up data that will cause an exception then you will get the coverage.  If not, you could use dependency injection to set a flag on the scheduler, and when that flag is set cause an exception by dereferencing a null pointer or similar.  My personal opinion is that is the wrong way to go, as you are adding code to production to allow tests to run.

This was selected as the best answer
nbnb

Hi Bob,

 

Thanks  a lot.

I have obtained the code coverage for the catch block.

 

 

 

 

Altaf MohammedAltaf Mohammed
HI,
I had some work around to cover the catch blck code in test class.
add the below line of code above the method in the class
public class myException extends Exception {}
then add this block code in try block of you class
if(test.isRunningTest())
    { 
        throw new myException('Error'); 
    }

the above code is good to be deployed in production as this block of code will run only when we run the test class. hope this helps you.
Note : Not yet tested with the scheduler class


Thanks
Altaf