Sub topic 6
Why High Coverage Can Still Mean Weak Tests
This chapter assumes you can read basic Apex (classes, methods, SOQL, DML) and that you know Salesforce requires 75% Apex code coverage to deploy to production. You also need to know that coverage counts executed lines. Here you will see why that number says little about correctness, and how to tell a meaningful test from one that only raises the percentage.
What coverage measures
Coverage records which lines ran during a test. It does not record whether the result was right. A line can run, return a wrong value, and still count as covered.
Here is the class under test:
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);
}
}A test that covers lines but checks nothing
This test calls the method and has no assertion:
@isTest
private class WeakDiscountCalculatorTest {
@isTest
static void coversLinesButChecksNothing() {
DiscountCalculator.apply(100, 10);
}
}It runs the happy path and passes. If someone changed the last line to amount + (amount * percent / 100), the result would be 110 instead of 90, and this test would still pass. The coverage number would not change.
A test that verifies behavior
The next class asserts a result and an expected exception.
@isTest
private class DiscountCalculatorTest {
@isTest
static void appliesPercentageDiscount() {
Decimal result = DiscountCalculator.apply(100, 10);
Assert.areEqual(90.00, result, 'A 10% discount on 100 should give 90');
}
@isTest
static void rejectsPercentOutOfRange() {
try {
DiscountCalculator.apply(100, 150);
Assert.fail('Expected an IllegalArgumentException');
} catch (IllegalArgumentException e) {
Assert.isTrue(e.getMessage().contains('between 0 and 100'), 'Unexpected message: ' + e.getMessage());
}
}
}The first method fails if the arithmetic is wrong. The second fails in two ways. It fails if no exception is thrown, because Assert.fail runs. It also fails if the message is wrong.
Common signs of a weak test
- No
Assertcalls, so nothing can fail. - Assertions that are always true, such as
Assert.isTrue(true)orAssert.isNotNull(result)when the value could be wrong but not null. - A
try/catchthat swallows an exception and has noAssert.failbefore it. The test passes whether or not the exception is thrown. - Only the happy path is tested, so validation branches go unverified. In this example, the two
throwlines are the ones left uncovered. - Only one record is tested, so bulk behavior is never exercised.
A quick check you can apply
For each test method, ask: "If I broke the code under test on purpose, would this test fail?" If the answer is no, add an assertion on the output, a record's field value, or an expected exception. You can try this by changing an operator in the class, running the tests, and seeing which ones fail. Then undo the change.
Pay particular attention to what the assertion compares against. A value computed by the same logic as the code under test proves nothing. Use a literal you worked out by hand, such as 90.00.
Exercise
A test calls DiscountCalculator.apply(100, 150) inside a try/catch for IllegalArgumentException. It has no Assert.fail after the call and no assertions in the catch block. Does the test pass if apply() stops throwing the exception? What should you add?
Show answer
Yes, it passes, because nothing fails when the exception is missing. Add Assert.fail('Expected an IllegalArgumentException') right after the call inside the try block. Also assert on the message in the catch block, as in rejectsPercentOutOfRange.