Sub topic 3
Verifying results with the Assert class
This chapter assumes you can write basic Apex classes, run SOQL queries and DML, and know that test methods use the @IsTest annotation. It covers how the Assert class checks results and how to write failure messages that help you.
Why assertions matter
A test method that runs without error proves only that the code did not crash. It does not prove the code did the right thing. An assertion compares what the code produced against what you expected. If the two differ, the assertion throws an AssertException, and the test fails.
Without assertions, a test can reach high code coverage and still miss broken logic. Assertions are what turn a test into a check.
The three-part call
The most common method is Assert.areEqual. It takes three arguments:
- The expected value.
- The actual value your code produced.
- A message that is shown when the assertion fails.
public class StringUtil {
public static String initials(String fullName) {
if (String.isBlank(fullName)) {
throw new IllegalArgumentException('Name is required');
}
String result = '';
for (String part : fullName.trim().split(' ')) {
if (String.isNotBlank(part)) {
result += part.substring(0, 1).toUpperCase();
}
}
return result;
}
}
@IsTest
private class StringUtilTest {
@IsTest
static void initialsAreUppercased() {
String result = StringUtil.initials('jane doe');
Assert.areEqual('JD', result, 'Initials should be uppercase letters of each word');
}
}The test calls initials('jane doe'), stores the result, and asserts that it equals 'JD'. If the method returned 'jd', the test would fail and show your message along with the expected and actual values.
Keep the order consistent: expected first, actual second. Swapping them still passes and fails correctly, but the failure output then labels the values the wrong way round, which misleads whoever reads it.
Writing useful failure messages
The message is the last argument. It is easy to skip, but it is what you read when a test fails months later in a deployment log.
A good message states what should have been true. Compare these two:
- Weak:
'Values are not equal' - Better:
'Initials should be uppercase letters of each word'
The weak message repeats what the assertion already tells you. The better one names the rule being checked, so you can see what broke without opening the code.
Where assertions go
Put the assertion after the code under test runs, and assert on a result you can observe. That might be a return value, a field on a record, or a count from a query. When the code saves data, re-query the record instead of trusting the in-memory object, because the query shows what the database holds.
@IsTest
static void accountNameIsSaved() {
Account acct = new Account(Name = 'Acme');
insert acct;
Account saved = [SELECT Name FROM Account WHERE Id = :acct.Id];
Assert.areEqual('Acme', saved.Name, 'Saved account should keep its name');
}Common mistakes
- No assertion at all. The test passes whenever the code does not throw. Coverage rises, but nothing is verified.
- Asserting something that is always true.
Assert.isTrue(true)or comparing a variable to itself can never fail. - Missing messages. The test still works, but failures are harder to diagnose.
- Several unrelated checks in one method. The first failure stops the method, so you do not see the others. Keep each test focused on one behavior, and use a few assertions only when they describe the same outcome.
How the pieces connect
The next chapter looks at the other Assert methods, such as isTrue, isNull, and fail, and when to choose each one.
Exercise
A test calls StringUtil.initials('ann lee') and the method returns 'AL'. Write the assertion line that checks this, including a message, and explain why re-querying matters when a test checks saved records.
Show answer
Assert.areEqual('AL', result, 'Initials should be uppercase letters of each word'); where result holds the return value. Re-querying matters because it reads the data as the database stored it, so the assertion confirms the save worked instead of only checking the in-memory object you already had.
Exercise
Why can a test with 100% code coverage still fail to catch a bug?
Show answer
Coverage only records which lines ran. If the test has no assertions, or asserts something that cannot fail, a wrong result still lets the test pass.