I have written a test class to cover a trigger and got 80% code coverage but am unable to cover try catch block in the trigger.
-
4Yes please, post your relevant code in the question.– Santanu HalderCommented Feb 1, 2017 at 11:13
-
1Make sure code throws exception in one use case so it covers the catch block.– Krishna BidwaiCommented Feb 1, 2017 at 11:14
-
2post your trigger and helper class– cloudZigZagCommented Feb 1, 2017 at 11:18
-
@Laxman kindly don't use regards, thanks in your question or answer posts. Secondly, do post your code, we won't be able to help you appropriately without looking at what you have tried so far.– MahmoodCommented Mar 6, 2017 at 8:09
1 Answer
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 a exception built into the actual code you're testing. This involves using Test.isRunningTest to set up an arbitrary failure.
For reference, I am also providing you the following link that will help you:
http://nahuelbergamo.ghost.io/2016/01/11/adding-test-coverage-for-exception-code/