Apex unit test data and test case design
How Apex tests isolate and create their own data with factories, @testSetup and CSV static resources, and how to design positive, negative, permission-based and bulk test cases that prove behaviour instead of chasing a coverage number.
Describe the appropriate approaches to building test data strategy and unit test to ensure successful code (positive, negative, permission-based, large data volume).
~8 min read · 10 practice questions ·
What the exam expects
- Recommend how tests get their data: isolated, self-created records through a factory,
@testSetupor a CSV static resource, notSeeAllData=true. - Design positive, negative, permission-based and bulk test cases, and say what each proves.
- Separate what unit tests prove (bulk-safe logic near governor limits) from what load and volume tests in a Full sandbox prove (real volumes, skew, contention).
- Diagnose data-dependent and parallel-execution failures such as UNABLE_TO_LOCK_ROW.
- Keep tests focused, mock callouts and keep personal data out of exception messages.
Key terms: test data isolation (tests see only the data they create, plus setup and metadata objects), test data factory (an @IsTest class whose methods build records), @testSetup (a method that creates records once per class), negative test (proves invalid input or failure is handled), System.runAs (runs a block as a given user so that user's sharing and permissions apply).
Key facts
- Detail
- API 24.0+ tests see only their own data plus objects like User, Profile, RecordType
- Detail
- Under the same data restriction
- Detail
- Tests don't commit, so no deletes needed
@testSetup limits- Detail
- One per class; not with
SeeAllData=true; fatal error fails the class
- Detail
- At least 75% of Apex; every trigger has some coverage
- Detail
- Use at least 20 records
runAs scope- Detail
- Test methods only; ignores license limits; counts as DML
- Detail
- Disable Parallel Apex Testing in Apex Test Execution options
- Why must a test that runs in every environment build its own records?
- When does a change made by one test method to
@testSetuprecords affect the next method? - Why should a negative test check the caught exception's type and message?
- Which records can an API 24.0+ test read without
SeeAllData?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Development Lifecycle and Deployment Architect pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 26 lessons of the Platform Development Lifecycle and Deployment Architect guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (10 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams