SF-PD2 Testing, Debugging, and Deployment Practice Question
A developer is writing a test class for a service that makes a callout to an external REST API. The test must not perform a real callout, and the developer needs to verify that the service correctly handles both a successful response and an error response. Which approach should the developer use?
⚠ Common exam trap
The trap here is believing that Test.startTest or direct use of Http classes can substitute for a mock, when tests must register an HttpCalloutMock to avoid real callouts and to control responses.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use Test.setMock with an implementation of HttpCalloutMock that returns different responses based on the request endpoint
HttpCalloutMock registered with Test.setMock intercepts callouts during tests and returns the response the mock defines. By inspecting the request endpoint or body, the mock can return a success payload for one path and an error status for another, letting the test validate both branches. This keeps production code free of test-specific branches and satisfies the no-real-callout rule.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use HttpRequest and Http directly in the test class to simulate the external service response
Why it's wrong here
Instantiating HttpRequest and Http in a test class and sending a request still attempts a real callout, which is not allowed in tests without a mock. The test would fail with a callout exception. Simulating a response requires a mock implementation registered with Test.setMock, not direct use of the HTTP classes.
- ✗
Use Test.isRunningTest to conditionally return hardcoded responses from the service class instead of performing the callout
Why it's wrong here
Branching on Test.isRunningTest inside production code pollutes the service with test-specific logic and does not exercise the actual callout handling code. It also makes the test less meaningful because the real request-building and response-parsing logic may be bypassed. The proper mechanism is a mock, which keeps production code clean and still validates the callout path.
- ✓
Use Test.setMock with an implementation of HttpCalloutMock that returns different responses based on the request endpoint
Why this is correct
Test.setMock with HttpCalloutMock registers a mock callout handler for the test. The mock can inspect the request, including the endpoint or body, and return a success or error response accordingly. This allows the test to exercise both paths without making real callouts, and it respects the requirement that callouts in tests must be mocked.
- ✗
Use Test.startTest and Test.stopTest to wrap the callout and rely on the test context to intercept external requests
Why it's wrong here
Test.startTest and Test.stopTest limit governor limits and execute asynchronous work, but they do not intercept callouts. A real callout attempted during a test without a mock causes the test to fail. Wrapping the callout in these methods does not provide a mock response, so the success and error paths cannot be exercised reliably.
About these practice questions
Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.