SF-PD2 Advanced Developer Fundamentals Practice Question
A developer is writing a unit test for a service class that performs a callout to an external REST endpoint. The test needs to verify that the class correctly parses a JSON response and updates a record. Which approach should the developer take to simulate the callout response in the test?
⚠ Common exam trap
The trap here is assuming that Test.startTest or @TestVisible can simulate callouts, when only HttpCalloutMock provides that capability.
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
✓
Implement the HttpCalloutMock interface and use Test.setMock(HttpCalloutMock.class, new MyMock());
The correct approach is to implement the HttpCalloutMock interface and register it with Test.setMock. This is the standard Salesforce pattern for simulating callouts in unit tests. It allows the developer to return a controlled response, ensuring the test focuses on the parsing and update logic without making real HTTP requests. The other options either do not simulate callouts or require undesirable changes to production code.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Implement the HttpCalloutMock interface and use Test.setMock(HttpCalloutMock.class, new MyMock());
Why this is correct
This is the correct approach. The HttpCalloutMock interface allows the developer to define a mock response for the callout. By calling Test.setMock, the test instructs the runtime to use the mock instead of making an actual HTTP request. This ensures the test is isolated, fast, and does not depend on external services, and it allows verification of the parsing logic with controlled data.
- ✗
Use the @TestVisible annotation to inject a mock response into the service class.
Why it's wrong here
The @TestVisible annotation allows test methods to access private members of a class, but it does not provide a mechanism to simulate HTTP callouts. While it could be used to inject a mock response object, the developer would still need to bypass the actual callout, which requires the HttpCalloutMock interface. This approach alone is insufficient and does not address the core requirement.
- ✗
Use Test.startTest() and Test.stopTest() around the callout to simulate the response.
Why it's wrong here
Test.startTest and Test.stopTest are used to reset governor limits and execute asynchronous code, but they do not simulate callouts. Without a mock, the callout would attempt a real HTTP request, which is not allowed in tests and would cause a fatal error. This approach fails to provide a simulated response and does not isolate the test from external dependencies.
- ✗
Replace the callout with a hardcoded response using a static variable in the service class.
Why it's wrong here
Hardcoding a response in a static variable would require modifying the production code to support testing, which is not a best practice. It couples the test to implementation details and does not test the actual callout logic. Additionally, it would not work if the class is already deployed and cannot be changed for testing purposes. This approach is brittle and not recommended.
About these practice questions
This SF-PD2 question is part of Courseiva's 226-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.