Testing RESTful web services with a custom mock
We have just seen how we can use a static resource and the StaticResourceCalloutMock
and MultiStaticResourceCalloutMock
classes to provide a mock implementation and response for a web service callout. We can also implement the HttpCalloutMock
class ourselves to provide a custom mock to provide the responses we want. This interface has a single respond
method that we need to implement. An example implementation is shown here:
@isTest public with sharing class AccountServiceMock implements HttpCalloutMock { public HttpResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('{name:"Acme Anvils inc."}'); ...