1

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.

  • 4
    Yes please, post your relevant code in the question. – Santanu Halder Feb 1 '17 at 11:13
  • 1
    Make sure code throws exception in one use case so it covers the catch block. – Krishna Bidwai Feb 1 '17 at 11:14
  • 2
    post your trigger and helper class – cloudZigZag Feb 1 '17 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. – Mahmood Mar 6 '17 at 8:09
1

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.