Sub topic 4
Anatomy of a Test Class and Test Method
This chapter assumes you can read basic Apex (classes, methods, collections, SOQL, DML) and know what a trigger is. You will learn the parts of a test class and how to read one. The next chapters cover assertions and test data in more depth.
What a test class is
A test class is an Apex class marked with the @isTest annotation. It holds test methods, which are static methods that the platform runs on demand. Test code does not count against your org's Apex character limit, and the test class itself is excluded from coverage calculations.
A test method must follow these rules:
- It is
static. - It returns
void. - It takes no parameters.
- It carries the
@isTestannotation.
Test classes are usually private, because nothing else needs to call them. The name conventionally ends in Test, such as DiscountCalculatorTest.
The class under test
Here is a small class with 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);
}
}The test class
@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());
}
}
}Each test method follows a common three-step shape:
- Arrange. Build the inputs. Here the inputs are literals. Later they will be records.
- Act. Call the code under test once.
- Assert. Check the result or the expected exception.
Keep each method focused on one behavior. The names say what the test proves, so a failure report is readable without opening the code.
How the exception test works
The second test deserves a closer look. Assert.fail sits directly after the call that should throw. If apply wrongly returns normally, execution reaches Assert.fail and the test fails. If it throws the right exception type, control jumps to the catch block, where the test checks the message.
Without Assert.fail, this test would pass whether or not an exception was thrown. That is a silent hole.
What the platform does around your test
Each test method runs in its own transaction. Data you insert in one method is rolled back when it ends, so methods do not see each other's records. The test also gets a fresh set of governor limits. By default, tests do not see the org's existing data. That isolation is deliberate, and the chapter on test data explains how to build what you need.
Common mistakes
| Mistake | Effect |
|---|---|
| Test method is not `static` or returns a value | The class does not compile or the method is not run as a test |
| Missing `@isTest` on the method | The method is not run as a test |
| No assertion after the call | The test passes even if the result is wrong |
| `try`/`catch` without `Assert.fail` | The test passes even when no exception is thrown |
| One method checking many unrelated behaviors | A failure is hard to diagnose |
Exercise
In rejectsPercentOutOfRange, what happens if you delete the Assert.fail line and DiscountCalculator.apply(100, 150) stops throwing an exception? Does the test pass or fail, and why?
Show answer
The test passes. With no exception thrown, the catch block never runs and nothing else checks the outcome. Assert.fail is what turns "no exception" into a failure.
Exercise
Write the signature line of a valid test method named handlesZeroPercent, and list the three steps its body should follow.
Show answer
@isTest static void handlesZeroPercent(). The body should arrange the inputs, act by calling the code under test once, and assert the expected outcome.