Courseiva

SF-PD2 Advanced Developer Fundamentals Practice Question

A developer needs to write a unit test for an Apex class that performs a callout to an external REST endpoint. The test must not make a real HTTP request and must simulate a successful response with a specific status code and body. Which approach should the developer use?

⚠ Common exam trap

The trap here is believing Test.startTest and Test.stopTest suppress callouts, when they only manage limits and asynchronous execution.

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 HttpCalloutMock and register it with Test.setMock before invoking the callout.

HttpCalloutMock is the platform-provided mechanism for stubbing callouts in tests. Implementing respond() and registering the mock with Test.setMock allows the test to return a controlled HttpResponse with a specified status code and body. Other techniques do not prevent the real callout or produce the desired simulated response.

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 System.assert with a try-catch block to intercept the callout exception and convert it into a fake response.

    Why it's wrong here

    Catching a callout exception does not produce a simulated HttpResponse with a chosen status code and body. The code under test would still not receive the expected response object, so the test would not validate success-path logic. Assertions and exception handling are not substitutes for a registered mock.

  • ✓

    Implement HttpCalloutMock and register it with Test.setMock before invoking the callout.

    Why this is correct

    HttpCalloutMock is the standard interface for simulating callouts in Apex tests. Implementing respond() lets the developer return a fabricated HttpResponse with a chosen status code and body, and Test.setMock(HttpCalloutMock.class, mockInstance) registers it for the test. This prevents real network calls and makes the test deterministic.

  • ✗

    Use Test.startTest and Test.stopTest around the callout to suppress the actual HTTP request.

    Why it's wrong here

    Test.startTest and Test.stopTest bound the code under test to reset governor limits and run asynchronous work, but they do not suppress or simulate callouts. A callout inside a test without a registered mock still throws a callout exception, so this approach does not satisfy the requirement.

  • ✗

    Set the endpoint URL to a localhost address so the callout fails and returns a simulated error.

    Why it's wrong here

    Pointing the endpoint at localhost does not create a simulated success response; it either fails the callout or throws an exception. Tests cannot rely on network behavior, and the requirement is to simulate a successful response with a specific status and body, which this approach cannot guarantee.

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 →

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.