Sub topic 13
A note on WebServiceMock for SOAP
This chapter assumes you have read the earlier chapters. You should know why tests cannot make real callouts, and how HttpCalloutMock and Test.setMock work together. It also helps to know that Apex generates classes from a WSDL file.
Why HttpCalloutMock does not fit SOAP
Some services use SOAP instead of REST. In Apex you usually reach them through classes generated from a WSDL (Web Services Description Language) file. You import the WSDL in Setup, and Salesforce generates the classes.
The generated classes do not build an HttpRequest that you can see. They hide the HTTP details behind a stub class and typed request and response objects. Because of this, HttpCalloutMock does not apply. Apex provides a second interface for this case, WebServiceMock.
| Row | HttpCalloutMock | WebServiceMock |
|---|---|---|
| Used for | `Http.send` callouts | Callouts from WSDL-generated classes |
| Method to implement | `respond(HttpRequest)` | `doInvoke(...)` with nine parameters |
| Return mechanism | Returns an `HttpResponse` | Puts a typed object into a map |
| Registered with | `Test.setMock(HttpCalloutMock.class, ...)` | `Test.setMock(WebServiceMock.class, ...)` |
The rest of the workflow is the same. You register the mock before the callout, run the code between Test.startTest and Test.stopTest, and assert on the result.
Implementing WebServiceMock
The interface has one method, doInvoke. It does not return a value. Instead, you create the generated response object and store it in the response map under the key response_x.
The examples below assume a WSDL was imported into a class named weatherWsdl. The class contains a stub named WeatherSoap with a getCondition(String city) method. It also contains a response type named GetConditionResponse_element with a GetConditionResult field. Your generated names will differ.
@IsTest
public class WeatherSoapMock implements WebServiceMock {
public void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType
) {
weatherWsdl.GetConditionResponse_element result =
new weatherWsdl.GetConditionResponse_element();
result.GetConditionResult = 'Sunny';
response.put('response_x', result);
}
}The other parameters describe the call that was made. They are request, endpoint, soapAction, requestName, and the response metadata. A simple mock can ignore them. A mock that serves several operations can branch on requestName or cast request to check what was sent.
Registering and using the mock
Registration works as it does for HTTP mocks. Only the type argument changes.
@IsTest
private class WeatherSoapTest {
@IsTest
static void returnsConditionFromSoapService() {
Test.setMock(WebServiceMock.class, new WeatherSoapMock());
Test.startTest();
weatherWsdl.WeatherSoap stub = new weatherWsdl.WeatherSoap();
String condition = stub.getCondition('Oslo');
Test.stopTest();
Assert.areEqual('Sunny', condition, 'The condition should come from the mock');
}
}Common errors
- Using the wrong interface. If you register an
HttpCalloutMockfor generated SOAP code, the callout is not intercepted. The test then fails with an error that callouts are not allowed in tests. - Wrong map key. The generated stub reads the response from the key
response_x. Any other key gives you a null result or a cast error. - Wrong response type. The object you put in the map must be the generated
..._elementclass for that operation. A different type causes a type exception. - Registering too late. As with HTTP mocks,
Test.setMockmust run before the callout.
Exercise
A test calls a WSDL-generated stub and fails with an error saying callouts are not allowed in tests. The test calls Test.setMock(HttpCalloutMock.class, new WeatherServiceMock(200, '{}')) first. What is wrong, and how do you fix it?
Show answer
Generated SOAP stubs do not go through Http.send, so an HttpCalloutMock is never used. Write a class that implements WebServiceMock. In doInvoke, create the generated response element and put it in the response map under response_x. Then register it with Test.setMock(WebServiceMock.class, new WeatherSoapMock()) before the callout.