Sub topic 1
Anatomy of an Apex test class
Prerequisites
This chapter assumes you can read and write basic Apex: classes, methods, collections, and control flow. You should know SOQL queries and the insert and update DML statements. You should also know the Account and User objects and that Apex runs under governor limits. No earlier testing experience is needed.
What a test class is
A test class is an Apex class that holds test methods. Each test method calls your production code and checks the result. Salesforce runs these methods on demand, during deployments, and before a package is released.
Two annotations mark test code:
@IsTeston a class marks it as test code. Test classes do not count against the org's Apex code size limit.@IsTeston a method marks it as a test method. Test methods must bestatic, take no parameters, and return nothing.
By convention, a test class is private and named after the class it tests, with Test added at the end.
A first test
The example below has a small class and a test class for it. The test calls initials, then compares the actual result with the expected one.
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');
}
}In your org, put each class in its own file. Here they are shown together for reading.
The three parts of a test method
Most test methods follow the same shape, often called arrange, act, assert.
- Arrange: create the input data and put the system in the state you need.
- Act: call the code you are testing.
- Assert: compare the actual outcome with what you expected.
In initialsAreUppercased, the input string is the arrange step, the call to StringUtil.initials is the act step, and Assert.areEqual is the assert step. A test with no assert step can still run the code, but it cannot fail when the code is wrong.
How a test run behaves
Three facts from this flow matter in practice:
- A method passes if it finishes without an uncaught exception. A failed assertion throws an exception, so it fails the method.
- Records that a test inserts are rolled back when the method ends. They never become permanent data in the org.
- By default, tests do not see the org's existing records. Your test must create the data it needs. Later chapters cover this isolation and
@TestSetupin detail.
Naming and structure habits
A good method name states the behavior being checked. initialsAreUppercased tells a reader what broke when it fails. test1 does not.
Keep each method focused on one behavior. When two behaviors fail for different reasons, separate methods show you which one broke.
Always write a failure message as the last argument of an assertion. The message appears in the test results, so it should say what you expected.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Declaring a test method as non-static or with parameters | The method is not treated as a valid test method and fails to save or run | Use `static void name()` with no parameters |
| Querying existing org records | The query returns nothing, because tests do not see org data by default | Create the records inside the test or in `@TestSetup` |
| Omitting the failure message | Failures are harder to diagnose | Add a message that states the expectation |
| Testing several behaviors in one method | One failure hides the others | Split into focused methods |
Exercise
A teammate writes @IsTest public void checkName() { ... } in a test class. The code never runs as a test. What must change, and why?
Show answer
Make the method static: @IsTest static void checkName(). Test methods must be static, take no parameters, and return nothing. The method also needs at least one Assert call so that it can fail when the behavior is wrong.
Exercise
Name the three steps of a typical test method, and say which step in initialsAreUppercased each line belongs to.
Show answer
Arrange, act, assert. The input string 'jane doe' is the arrange step. The call StringUtil.initials('jane doe') is the act step. The Assert.areEqual('JD', result, ...) line is the assert step.