You need to sign in to do that
Don't have an account?
nb
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
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
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.
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
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
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.
Hi Bob,
Thanks a lot.
I have obtained the code coverage for the catch block.
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 then add this block code in try block of you class
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