Writing and running Apex and Flow tests
How to structure @IsTest classes, pick a source of test data (@TestSetup, a data factory, Test.loadData, SeeAllData), isolate limits and async work with Test.startTest/stopTest, mock callouts, test triggers and controllers, meet the 75% coverage rule, and test flows with Test Mode or the debugger.
Write and execute tests for triggers, controllers, classes, and flows using various sources of test data.
~8 min read · 25 practice questions ·
What the exam expects
- Structure test classes and methods for classes, triggers and Visualforce controllers.
- Choose a source of test data (records built in the test,
@TestSetup, a test data factory,Test.loadData, orSeeAllData=true) and explain data isolation. - Use
Test.startTest()/Test.stopTest()to isolate governor limits and force asynchronous work (future, Queueable, batch) to finish before assertions. - Replace callouts and dependencies with
HttpCalloutMockor the Stub API, test as other users withSystem.runAs(), and prove bulk and negative paths with assertions. - State the 75% coverage rule and what counts toward it; test flows with Test Mode or the Flow Builder debugger.
Key terms: test class (a class annotated @IsTest that holds test methods); data isolation (tests see only records they create, by default); test setup method (@TestSetup, builds shared records once per class); mock (a test-only stand-in that returns a canned response); code coverage (executable lines run by tests divided by all executable lines).
Key facts
- Detail
- Only records the test creates, plus setup objects such as User, Profile, RecordType
SeeAllData=true- Detail
- Method-level
SeeAllData=falseis ignored; records are still rolled back
startTest/stopTest- Detail
- Once each per test method; new limits between; async runs at
stopTest
- Detail
- Only one execution of
execute; limit records with the scope parameter
- Detail
Test.setMock(HttpCalloutMock.class, new MyMock())before the callout
- Detail
- Static (including future) and private methods, properties, triggers
System.runAs()- Detail
- Enforces the user's sharing and permissions; test methods only
- Detail
- At least 75% of Apex, all tests pass, every trigger has some coverage
- Detail
- Test classes, comments, blank lines, lone
System.debug()lines
- Detail
- Autolaunched and record-triggered flows; other types use the debugger
Sources for this section (10)
- Isolation of Test Datadeveloper.salesforce.com (opens in a new tab)
- Using the isTest(SeeAllData=True) Annotationdeveloper.salesforce.com (opens in a new tab)
- Test Classdeveloper.salesforce.com (opens in a new tab)
- Using Batch Apexdeveloper.salesforce.com (opens in a new tab)
- Testing HTTP Calloutsdeveloper.salesforce.com (opens in a new tab)
- Build a Mocking Framework with the Stub APIdeveloper.salesforce.com (opens in a new tab)
- Using the runAs Methoddeveloper.salesforce.com (opens in a new tab)
- Testing Best Practicesdeveloper.salesforce.com (opens in a new tab)
- Testing and Code Coveragedeveloper.salesforce.com (opens in a new tab)
- Testing Your Flow Before Activationhelp.salesforce.com (opens in a new tab)
- Why does a query for records you can see in the sandbox return zero rows inside a test?
- When does a future method called in a test actually run?
- Why keep test data builders in an `@IsTest` utility class instead of a plain public class?
- Which flow types use Test Mode rather than the Flow Builder debugger?
- What minimum coverage must Apex have before it can be deployed to production?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 21 lessons of the Platform Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (25 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams