Sub topic 5
Verifying Outcomes with the Assert Class
This chapter assumes you can read basic Apex, write a test method with @isTest, and run tests in a sandbox, scratch org or Developer Edition org. You will learn how to use the Assert class so that a test fails when behavior is wrong, not only when code throws an error.
Why assertions matter
Salesforce counts a line as covered when a test executes it. It does not check that the line did the right thing. An assertion is a statement that compares what happened with what you expected. If the two differ, the test fails.
Without assertions, a test passes whenever the code runs without an unhandled exception. A wrong calculation still passes.
The core Assert methods
The Assert class is in the System namespace, so you call it directly. Each method takes the values to check and an optional message that appears when the check fails.
| Method | Passes when |
|---|---|
| `Assert.areEqual(expected, actual, msg)` | The two values are equal |
| `Assert.areNotEqual(notExpected, actual, msg)` | The two values differ |
| `Assert.isTrue(condition, msg)` | The condition is `true` |
| `Assert.isFalse(condition, msg)` | The condition is `false` |
| `Assert.isNull(value, msg)` | The value is `null` |
| `Assert.isNotNull(value, msg)` | The value is not `null` |
| `Assert.fail(msg)` | Never. It always fails the test |
Put the expected value first and the actual value second. When a test fails, the platform reports them in that order, so a swapped pair gives a misleading message.
Asserting a result
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);
}
}The happy path test checks the returned value and the invalid-input test checks the 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());
}
}
}Why Assert.fail sits inside the try block
In rejectsPercentOutOfRange, the call is expected to throw. If it does, control jumps to the catch block and Assert.fail never runs. If the code wrongly returns normally, Assert.fail runs and fails the test.
Without that line, the test would pass in both cases, because a method that throws nothing never enters the catch block and nothing else marks a failure.
The catch block then asserts on the message. This confirms the right validation branch fired, not just any IllegalArgumentException.
Writing useful messages
The last argument is what you read at 5 p.m. when a deployment fails. Say what should be true, not what the line does.
- Weak:
'Values not equal' - Better:
'A 10% discount on 100 should give 90'
For Assert.isTrue with a computed value, include that value in the message, as the exception test does with e.getMessage().
Common errors
- Asserting nothing. A test that only calls the method raises coverage but proves nothing.
- Swapped arguments. Failure output then shows the expected and actual values the wrong way round.
- A missing
Assert.fail. An exception test with noAssert.failpasses even when no exception is thrown. - Catching too broadly. Catching
Exceptionaccepts any error, including unrelated ones. Catch the specific type and check the message. - Legacy
System.assertcalls.System.assertEqualsandSystem.assertstill work. In new code, prefer theAssertclass, whose method names state the intent.
Exercise
A test calls DiscountCalculator.apply(100, -5) inside a try block and catches IllegalArgumentException, but the try block has no Assert.fail line. Someone later removes the range check from apply. Does the test still pass, and why?
Show answer
Yes, it still passes. With the check removed, apply returns a value instead of throwing, so the catch block never runs. Nothing else in the test marks a failure, so it passes silently. Adding Assert.fail('Expected an IllegalArgumentException') after the call makes the test fail in that case.
Exercise
Rewrite Assert.areEqual(result, 90.00) so it uses the correct argument order and a helpful message.
Show answer
Assert.areEqual(90.00, result, 'A 10% discount on 100 should give 90'); The expected value comes first, the actual value second, and the message states what should be true.