Tuesday, May 10, 2011

Avoid single-line Salesforce Apex if expressions for code coverage

In Salesforce Apex a single line if expression should be avoided if you want to ensure full code coverage by the automated tests.

I found that Apex code would show as having full test coverage even if the expression never evaluated to true. For example, the following would show as being completely tested even though the doSomething method was never called.

Boolean exampleCondition = false;
if (exampleCondition) { doSomething(); }