Anatomy of a Test Class
By the end of this lesson, you'll be able to:
- Identify the required annotations and structure of an Apex test class
- Explain why test classes don't count against your org's total Apex code limit
Prerequisites: None — this is the first lesson in the course.
The @isTest annotation
A class-level @isTest marks the whole class as test-only — it can't be called from non-test code. A method-level @isTest marks an individual test method. Test classes are automatically excluded from your org's 6MB total Apex code size limit, so writing plenty of tests never eats into your production code budget.
A minimal test class
Test class names conventionally end in Test. Each test method covers one scenario: it creates its own data, performs an action, and asserts the result — a shape you'll see repeated throughout this course.
A minimal passing test
@isTest
private class AccountHelperTest {
@isTest
static void greetReturnsAFormattedString() {
String result = AccountHelper.greet('Ada');
System.assertEquals('Hello, Ada!', result);
}
}
Test methods are static and take no parameters — Salesforce's test runner calls each one independently, in its own database transaction that's rolled back afterward.
Exercise
Write a minimal @isTest class named MathHelperTest with one test method that asserts MathHelper.square(4) returns 16.
Show hint
Both the class and the method need @isTest; use System.assertEquals(expected, actual).
Anatomy of a Test Class — Quick Check
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A test class is annotated @isTest, contains one or more methods annotated @isTest, and is excluded from your org's total Apex code size limit — Salesforce wants you to write plenty of tests without worrying about hitting that ceiling.