Apex mocks, stubs and advanced test techniques
Pick the right callout mock (HttpCalloutMock, static resource mocks, WebServiceMock), isolate classes with the Stub API and dependency injection, and control test context with @TestVisible, @testSetup, event delivery, fixed SOSL results, setCreatedDate, runAs and Assert-based negative and bulk tests.
Apply advanced techniques and tools for testing Apex classes and triggers, such as mocks and stubs.
~8 min read · 15 practice questions ·
What the exam expects
- Choose between
HttpCalloutMock,StaticResourceCalloutMock,MultiStaticResourceCalloutMockandWebServiceMockfor a given callout, and fix a test that does DML before a mocked callout. - Isolate a class with the Stub API (
Test.createStub()plusStubProvider), inject the stub, and name what the Stub API can't mock. - Control the test context:
@TestVisible,@testSetupand static variables,Test.loadData(),Test.setFixedSearchResults(),Test.setCreatedDate(), batch and event delivery, andSystem.runAs()with class sharing modes. - Write negative and bulk tests that follow Salesforce's testing best practices.
Key terms: mock (a test-only object that returns a canned callout response); stub (a runtime-generated subclass whose methods StubProvider.handleMethodCall() answers); dependency injection (handing a collaborator to a class instead of letting it new its own); test event bus (the separate bus that holds platform and change events in tests until they are delivered); static context (the static variables of a test class, reinitialized per test method).
This unit assumes the basics in the Platform Developer guide's Writing and running Apex and Flow tests (@IsTest, data isolation, startTest/stopTest, coverage).
Key facts
Test.setMock scope- Detail
- HTTP-class callouts and WSDL-generated code only
- Detail
MultiStaticResourceCalloutMock.setStaticResource(endpoint, name)
- Detail
WebServiceMock.doInvoke()putsresponse_xin the response map
- Detail
startTestbeforesetMock; DML outside the block
- Detail
- Static, private, properties, triggers, inner classes, system types, Batchable, private-only constructors
@testSetup- Detail
- One per class; static context reset before each test method
- Detail
- Empty results unless
Test.setFixedSearchResults()
- Detail
- Only one
executecall can be tested
- Detail
enableChangeDataCapture()first; up to 500 events delivered
runAs and called classes- Detail
- Called method uses its own class's sharing mode
- Detail
- At least 20 records
Sources for this section (11)
- Test Classdeveloper.salesforce.com (opens in a new tab)
- Static Resource Callout Mocksdeveloper.salesforce.com (opens in a new tab)
- Test Web Service Calloutsdeveloper.salesforce.com (opens in a new tab)
- DML and Mock Calloutsdeveloper.salesforce.com (opens in a new tab)
- Stub APIdeveloper.salesforce.com (opens in a new tab)
- Using Test Setup Methodsdeveloper.salesforce.com (opens in a new tab)
- SOSL in Unit Testsdeveloper.salesforce.com (opens in a new tab)
- Use Batch Apexdeveloper.salesforce.com (opens in a new tab)
- Test Change Event Triggersdeveloper.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)
- Why does a test that inserts an Account before a mocked callout fail?
- When does a stub from `Test.createStub()` actually run?
- Why is a static variable assigned in `@testSetup` null in a test method?
- What must a test call so SOSL returns a record it inserted?
- When must `Test.enableChangeDataCapture()` be called?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (15 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams