Test Web Service Callouts
Specify a Mock Response for Testing Web Service Callouts
When you create an Apex class from a WSDL, the methods in the auto-generated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the Apex runtime to send. Here are the steps in more detail.
First, implement the WebServiceMock interface and specify the fake response in the doInvoke method.
1global class YourWebServiceMockImpl implements WebServiceMock {
2 global void doInvoke(
3 Object stub,
4 Object request,
5 Map<String, Object> response,
6 String endpoint,
7 String soapAction,
8 String requestName,
9 String responseNS,
10 String responseName,
11 String responseType) {
12
13 // Create response element from the autogenerated class.
14 // Populate response element.
15 // Add response element to the response parameter, as follows:
16 response.put('response_x', responseElement);
17 }
18}Now that you have specified the values of the fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method. For the first argument, pass WebServiceMock.class, and for the second argument, pass a new instance of your interface implementation of WebServiceMock, as follows:
1Test.setMock(WebServiceMock.class, new YourWebServiceMockImpl());After this point, if a web service callout is invoked in test context, the callout is not made. You receive the mock response specified in your doInvoke method implementation.
This example shows how to test a web service callout. The implementation of the WebServiceMock interface is listed first. This example implements the doInvoke method, which returns the response you specify. In this case, the response element of the auto-generated class is created and assigned a value. Next, the response Map parameter is populated with this fake response. This example is based on the WSDL listed in Generated WSDL2Apex Code. Import this WSDL and generate a class called docSample before you save this class.
1@isTest
2global class WebServiceMockImpl implements WebServiceMock {
3 global void doInvoke(
4 Object stub,
5 Object request,
6 Map<String, Object> response,
7 String endpoint,
8 String soapAction,
9 String requestName,
10 String responseNS,
11 String responseName,
12 String responseType) {
13 docSample.EchoStringResponse_element respElement =
14 new docSample.EchoStringResponse_element();
15 respElement.EchoStringResult = 'Mock response';
16 response.put('response_x', respElement);
17 }
18}This method makes a web service callout.
1public class WebSvcCallout {
2 public static String callEchoString(String input) {
3 docSample.DocSamplePort sample = new docSample.DocSamplePort();
4 sample.endpoint_x = 'https://example.com/example/test';
5
6 // This invokes the EchoString method in the generated class
7 String echo = sample.EchoString(input);
8
9 return echo;
10 }
11}This test class contains the test method that sets the mock callout mode. It calls the callEchoString method in the previous class and verifies that a mock response is received.
1@isTest
2private class WebSvcCalloutTest {
3 @isTest static void testEchoString() {
4 // This causes a fake response to be generated
5 Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
6
7 // Call the method that invokes a callout
8 String output = WebSvcCallout.callEchoString('Hello World!');
9
10 // Verify that a fake result is returned
11 System.assertEquals('Mock response', output);
12 }
13}