Courseiva

SF-PD2 Testing, Debugging, and Deployment Practice Question

A developer is writing an Apex test method for a service class that makes a callout to an external REST API. The test must not perform the actual callout. The developer creates a static resource containing the JSON response and writes a mock class that implements HttpCalloutMock. Which statement must the developer include in the test method to register the mock before the callout is executed?

⚠ Common exam trap

Test-takers frequently confuse WebServiceMock (for WSDL-based callouts) with HttpCalloutMock (for REST callouts made with HttpRequest).

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

✓

Test.setMock(HttpCalloutMock.class, new MyApiMock());

Test.setMock is the only supported way to inject a mock implementation for HTTP callouts during Apex tests. It must be invoked before the callout is made, typically at the start of the test method. The mock's respond method then supplies the HttpResponse, allowing the developer to test success and error handling without external dependencies.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    HttpCalloutMock mock = new MyApiMock(); Test.startTest(); mock.respond(new HttpRequest());

    Why it's wrong here

    Simply instantiating the mock and invoking respond does not register it with the testing framework. Apex will still attempt a real callout during the test, which throws a System.CalloutException because callouts are not allowed in tests without a registered mock. The respond method is called internally by the framework only after Test.setMock is used.

  • ✗

    Test.setMock(WebServiceMock.class, new MyApiMock());

    Why it's wrong here

    WebServiceMock is used to mock Apex-generated web service callouts from WSDL2Apex, not REST callouts made with the Http or HttpRequest classes. Passing an HttpCalloutMock implementation to Test.setMock with the WebServiceMock type will throw a runtime error because the mock does not implement the expected interface. This option misidentifies the interface required for HTTP callout mocking.

  • ✓

    Test.setMock(HttpCalloutMock.class, new MyApiMock());

    Why this is correct

    Test.setMock registers the mock implementation for the HttpCalloutMock interface so that any callout made during the test is intercepted and returns the canned response instead of hitting the network. It must be called before the code under test invokes the callout, otherwise the test fails with a callout exception. This is the correct mechanism for isolating Apex callout logic in unit tests.

  • ✗

    Test.loadData(HttpCalloutMock.SObjectType, 'MyApiMock');

    Why it's wrong here

    Test.loadData is used to insert records from a static resource CSV file for a specified sObject type. It has no relationship to mocking callouts and cannot register a mock implementation. Using it here would attempt to load data for a nonexistent sObject type named after the interface, failing immediately and leaving the callout unmocked.

About these practice questions

One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.