+ Start a Discussion
giriraj lakheragiriraj lakhera 

how can i cover try catch block line in test unit ?

Any way or trick for it ?
Best Answer chosen by giriraj lakhera
umesh atryumesh atry
Hi Giriraj,

You just need to add following line at the end of your try block.

if(Test.isRunningTest()){
  Integer x=1/0;
  }

These lines throws exception and then catch block will be covered.

I think it will be help you.
 

All Answers

umesh atryumesh atry
Hi Giriraj,

You just need to add following line at the end of your try block.

if(Test.isRunningTest()){
  Integer x=1/0;
  }

These lines throws exception and then catch block will be covered.

I think it will be help you.
 
This was selected as the best answer
ManojjenaManojjena
Hi GiriRaj,

You have to create an exception in your test code to cover the catch block . You can create in varous ways according to your need .
One way here is there in Forum you can cehck  below link .
https://developer.salesforce.com/forums?id=906F0000000B1x5IAC

If you need any help please let me know .


 
giriraj lakheragiriraj lakhera
Thanks! I like this trick. :)
Sameer PrasonnSameer Prasonn
Yes, Giri Hope you enjoyed it a lot!!! :D :D