Writing Your First Automated Test
By the end of this lesson, you'll be able to:
- Explain what a unit test verifies and why manual verification alone doesn't scale
- Write a minimal @isTest class with one test method and one assertion
- Recognize that the projects starting next module should include a real test, not just an Execute Anonymous check
Prerequisites: "CPU Time and Heap Size"
The problem with checking by hand
Every exercise so far has been verified by running code in Execute Anonymous and reading the debug log — useful for learning, but it has a real limitation: nothing about that check survives. If the code changes next week, nobody re-runs it. Real, production Apex needs a way to verify "does this still work?" automatically, every time — not just once, by hand, right after writing it.
A minimal automated test
@isTest
private class MathCheckTest {
@isTest
static void additionProducesCorrectSum() {
Integer result = 2 + 2;
System.assertEquals(4, result, 'Expected 2 + 2 to equal 4');
}
}
@isTest— on the class, marks the whole class as test-only code — it never runs as part of normal execution, only when tests are run.private class MathCheckTest {—... Testis the standard naming convention: a class namedMathCheckTesttests something related to "math check."@isTest— on the method, marks an individual test; every test method isstatic.static void additionProducesCorrectSum() {— the test method.Integer result = 2 + 2;— sets up a scenario and runs the code.System.assertEquals(4, result, 'Expected 2 + 2 to equal 4');—System.assertEquals(expected, actual, message)compares what you expected against what the code actually produced, and fails the test with a clear message if they don't match.}— closes the method.}— closes the class.
That's the entire shape: set up a scenario, run the code, assert the actual result matches what you expected.
Testing something more realistic
@isTest
private class AccountNameCheckTest {
@isTest
static void accountIsCreatedWithTheGivenName() {
Account acc = new Account(Name = 'Acme Corp');
insert acc;
Account inserted = [SELECT Name FROM Account WHERE Id = :acc.Id];
System.assertEquals('Acme Corp', inserted.Name, 'Expected the inserted Account to keep its given Name');
}
}
@isTest— marks the class as test-only.private class AccountNameCheckTest {— the naming convention, applied to this scenario.@isTest— marks the test method.static void accountIsCreatedWithTheGivenName() {— the test.Account acc = new Account(Name = 'Acme Corp');— sets up the scenario.insert acc;—insert(Module 19) is exactly the tool you already know — the insert itself is the action, nothing more needed.- (blank line)
Account inserted = [SELECT Name FROM Account WHERE Id = :acc.Id];— a SOQL query (Module 20), reading back what was actually saved.System.assertEquals('Acme Corp', inserted.Name, 'Expected the inserted Account to keep its given Name');— asserts the actual result matches what was expected.}— closes the method.}— closes the class.
This is the same shape as the math example — set up, act, assert — just applied to Salesforce data instead of arithmetic. This lesson is only teaching how to wrap tools you already know in something that verifies itself automatically.
Why this matters starting right now
The next module\'s project — the course\'s first real trigger-based feature — is exactly the kind of code that most needs an automated test: logic that runs automatically, on data changes, potentially affecting many records at once. From here on, "I checked it in Execute Anonymous" is a fine first step while building, but it is not where verification should stop. The full "Apex Testing" module, much later in the course, goes deep — test data factories, bulk and edge-case testing, mocking callouts, meaningful coverage. This lesson's only job is to make sure the habit — write a test, don\'t just eyeball the debug log — starts now, not dozens of modules from now.
Exercise
Write a minimal @isTest class named StringCheckTest with one test method asserting that 'Hello'.toUpperCase() equals 'HELLO'.
Show hint
Follow the exact shape of the MathCheckTest example: @isTest class, @isTest static void method, one System.assertEquals call.
Exercise
Challenge: explain, as a comment, why an automated test is more valuable than an Execute Anonymous check for code that will be modified again in the future.
Show hint
Think about what happens to each kind of check the moment the underlying code changes.
Writing Your First Automated Test Quiz
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
Before the first real trigger-based project, this lesson establishes the automated-testing habit early — a minimal @isTest class, one assertion, so "did I check this?" starts meaning "is there a test for this?" from here on.