Sub topic 1
What Apex Code Coverage Is and Why Salesforce Enforces It
Prerequisites
This chapter assumes you can read basic Apex: classes, methods, collections, SOQL, and DML. You should know at a high level what a trigger is and what governor limits are. You also need access to a sandbox, scratch org, or Developer Edition org if you want to run the examples.
What code coverage is
Code coverage is a measure of how much of your Apex code runs while your tests run. Salesforce counts the executable lines in your classes and triggers. It then records which of those lines ran during a test. The result is a percentage for each class and trigger, and one overall percentage for the org.
If a class has 10 executable lines and your tests run 8 of them, that class has 80% coverage.
Coverage tells you which code your tests reached. It does not tell you whether that code did the right thing. The rest of this tutorial returns to that difference often.
Why Salesforce enforces it
Salesforce is a multitenant platform. Many customers share the same infrastructure, and your Apex runs next to theirs. The platform wants a baseline guarantee that code deployed to production has been run at least once before real users depend on it.
Enforcing a coverage minimum has three practical effects:
- Code that fails on its first run, such as a syntax-valid line that throws a null pointer exception, is more likely to be caught before production.
- Every deployment gives you a regression check, because the tests run again and can break when someone changes the code.
- The platform and its upgrades can be checked against customer code. Salesforce runs customer tests to spot behavior changes in its own releases.
The details of the numeric requirement are covered in the next chapter. Here it is enough to know that a minimum exists and that deployments to production can be blocked if you miss it.
A worked example
Consider this class. It has a happy path and two validation branches.
public with sharing class DiscountCalculator {
public static Decimal apply(Decimal amount, Decimal percent) {
if (amount == null || percent == null) {
throw new IllegalArgumentException('amount and percent are required');
}
if (percent < 0 || percent > 100) {
throw new IllegalArgumentException('percent must be between 0 and 100');
}
return amount - (amount * percent / 100);
}
}Suppose the only test calls DiscountCalculator.apply(100, 10). That call runs the first if condition, the second if condition, and the return line. It never runs either throw line. Those two lines are executable but untested, so they count against coverage.
Add a second test that calls apply(100, 150). Now the second throw runs, and only the first throw is still uncovered. A third test with a null argument would cover every line.
The numbers below show how coverage rises as each test is added.
| Tests that run | Lines executed | Coverage (of 6 executable lines) |
|---|---|---|
| `apply(100, 10)` | 4 | about 67% |
| plus `apply(100, 150)` | 5 | about 83% |
| plus `apply(null, 10)` | 6 | 100% |
The count here is simplified to six lines for teaching: the two if lines, the two throw lines, the return, and the method body that holds them. Salesforce reports the exact figure per class in the Developer Console and in test results.
What counts and what does not
Not everything in your org affects the percentage. Keep these points in mind:
- Executable statements count. Comments, blank lines, and declarations without logic do not.
- Test classes marked
@isTestare not counted toward your coverage. - Code must run in a test context. Calling it manually in anonymous Apex does not add to coverage.
A common surprise is that a line can be covered by a test that never checks its result. The second test method below reaches every line of the happy path and asserts nothing.
@isTest
private class WeakDiscountCalculatorTest {
@isTest
static void coversLinesButChecksNothing() {
DiscountCalculator.apply(100, 10);
}
}If someone changed the formula to amount + (amount * percent / 100), this test would still pass and still report coverage.
Exercise
A class has 20 executable lines. Your tests run 15 of them. What is the coverage, and does that number tell you whether the 15 lines produce correct results?
Show answer
Coverage is 75% (15 of 20). It does not tell you whether the results are correct. It only records that those lines executed. You need assertions in the tests to check outcomes.