Testing, Debugging, and Deployment20% of the exam

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.

Official exam objective
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, MultiStaticResourceCalloutMock and WebServiceMock for a given callout, and fix a test that does DML before a mocked callout.
  • Isolate a class with the Stub API (Test.createStub() plus StubProvider), inject the stub, and name what the Stub API can't mock.
  • Control the test context: @TestVisible, @testSetup and static variables, Test.loadData(), Test.setFixedSearchResults(), Test.setCreatedDate(), batch and event delivery, and System.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
Per-endpoint static bodies
Detail
MultiStaticResourceCalloutMock.setStaticResource(endpoint, name)
SOAP mock
Detail
WebServiceMock.doInvoke() puts response_x in the response map
DML then mocked callout
Detail
startTest before setMock; DML outside the block
Stub API can't mock
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
SOSL in tests
Detail
Empty results unless Test.setFixedSearchResults()
Batch in tests
Detail
Only one execute call can be tested
Change events in tests
Detail
enableChangeDataCapture() first; up to 500 events delivered
runAs and called classes
Detail
Called method uses its own class's sharing mode
Bulk trigger tests
Detail
At least 20 records
Sources for this section (11)
  • 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

Practice for the Platform Developer II exam