2

This is not a big issue for now because I was able to write test classes that covers the rest of my triggers, but I'm noticing that for some of my triggers in the developer console that lines like these below are not covered, causing code coverage to be less than 100%. Just wondering if anyone can help with this, thanks!

       catch (DMLException e) {
        for (Opportunity opp: trigger.new) {
            opp.addError(e.getDmlMessage(0));

1 Answer 1

5

It may not be possible to test all error handling scenarios. This is why the code coverage requirement is 75% and not 100%. You must be able to anticipate errors that are not possible from your own code but might be possible after an administrator adds a validation rule, etc.

I've always said that, based on my observations, writing test methods follows the Pareto principle. In other words, 20% of the code you could write for test methods should cover 80% of your code, and the remaining 80% of the code you would write for test methods would be needed to cover the remaining 20%.

Covering 100% is often impossible, and most organizations recognize this. Even covering 90% requires probably close to at least 2 times more than required to cover 80%. This is because your main execution line should be around 80%, while unexpected exceptions should generally be in the remaining 20%.

That said, you should focus on setting a goal of 85% or 90%. If your code is less than 10 lines long, then that class or trigger will probably be less than 80% with proper error handling-- don't worry about it, because it's likely that you'd either have to write test code in your class or trigger (e.g. if(Test.isRunningTest() && TestUtil.causeFailure) { throw new Exception('Test Exception'); }), which is a bad practice, or you'd have to not write any error handling code, which is also impractical in production code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .