Courseiva

SF-PD2 Advanced Developer Fundamentals Practice Question

A developer needs to write a test method for an Apex class that performs a callout to an external web service. The test must not make an actual callout and should simulate a successful response. Which approach should be used?

⚠ Common exam trap

The trap here is thinking that simply skipping the callout with a conditional check is sufficient, when in fact you need to simulate a response to properly test the callout handling code.

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

Test.setMock with an HttpCalloutMock implementation is the standard way to simulate callouts in Apex tests. It allows you to define the response that the callout should return, so the test can verify the logic that processes the response. This approach ensures that no actual callout is made and that the test is deterministic and fast.

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 Test.isRunningTest() to conditionally skip the callout

    Why it's wrong here

    Test.isRunningTest() can be used to bypass callouts during tests, but it does not simulate a response. The code would need to handle the absence of a response, which may not test the actual callout handling logic. This approach is not recommended for testing callout functionality.

  • ✗

    Use HttpRequest with a mock endpoint URL

    Why it's wrong here

    Using a mock endpoint URL does not prevent the callout from being attempted. The test would still try to make an HTTP request to that URL, which is not allowed in Apex tests and would result in a callout exception. There is no built-in mechanism to intercept the request without Test.setMock.

  • ✗

    Use Database.executeBatch with a mock callout handler

    Why it's wrong here

    Database.executeBatch is for asynchronous batch processing and has no relation to mocking callouts. It cannot intercept HTTP requests or simulate responses. Using it in a test for callouts would not prevent the callout and would likely cause the test to fail due to uncommitted work or callout limits.

  • ✓

    Use Test.setMock with an implementation of HttpCalloutMock

    Why this is correct

    Test.setMock allows you to specify a mock implementation for HTTP callouts. By implementing the HttpCalloutMock interface, you can return a fabricated HttpResponse with the desired status code and body. This prevents actual callouts during tests and enables testing of callout logic in isolation.

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 →

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.