PDI · Question #17
A developer wrote Apex code that calls out to an external system. How should a developer write the test to provide test coverage?
The correct answer is B. Write a class that implements the HTTPCalloutMock interface. To provide test coverage for Apex code that makes HTTP callouts to an external system, a developer should write a mock class that implements the HttpCalloutMock interface.
Question
A developer wrote Apex code that calls out to an external system. How should a developer write the test to provide test coverage?
Options
- AWrite a class that extends HTTPCalloutMock.
- BWrite a class that implements the HTTPCalloutMock interface.
- CWrite a class that implements the WebserviceMock interface.
- DWrite a class that extends WebserviceMock
How the community answered
(41 responses)- A2% (1)
- B95% (39)
- D2% (1)
Why each option
To provide test coverage for Apex code that makes HTTP callouts to an external system, a developer should write a mock class that implements the `HttpCalloutMock` interface.
`HttpCalloutMock` is an interface, not a class, and therefore must be implemented by a class, not extended.
Implementing the `HttpCalloutMock` interface is the standard and required approach for simulating responses from external HTTP callouts in Apex tests, preventing actual network calls and ensuring reliable and fast test execution.
The `WebserviceMock` interface is specifically used for mocking WSDL-based web service callouts, not for HTTP REST callouts.
`WebserviceMock` is an interface, not a class, and is intended for WSDL-based web service callouts, making it inappropriate for testing HTTP REST callouts.
Concept tested: Mocking HTTP callouts in Apex tests
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_http_testing.htm
Topics
Community Discussion
No community discussion yet for this question.