Search

Wednesday 7 November 2012

Testing Web Service Callouts

Generated code is saved as an Apex class containing the methods you can invoke for calling the Web service. To deploy or package this Apex class and other accompanying code, 75% of the code must have test coverage, including the methods in the generated class. By default, test methods don’t support Web service callouts and tests that perform Web service callouts are skipped. To prevent tests from being skipped and to increase code coverage, Apex provides the built-in WebServiceMock interface and theTest.setMock method that you can use to receive fake responses in a test method.

Specifying 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 that the Apex runtime should send. Here are the steps in more detail.
First, implement the WebServiceMock interface and specify the fake response in the doInvoke method.
global classYourWebServiceMockImplimplements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {

        // Create response element from the autogenerated class. 
    
        // Populate response element. 
    
        // Add response element to the response parameter, as follows: 
    
        response.put('response_x', responseElement); 
   }
}
Note
  • The class implementing the WebServiceMock interface can be either global or public.
  • You can annotate this class with @isTest since it will be used only in test context. In this way, you can exclude it from your organization’s code size limit of 3 MB.
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:
Test.setMock(WebServiceMock.class, newYourWebServiceMockImpl());
After this point, if a Web service callout is invoked in test context, the callout is not made and you receive the mock response specified in your doInvoke method implementation.
Note
If the code that performs the callout is in a managed package, you must call Test.setMock from a test method in the same package with the same namespace to mock the callout.
This is a full example that shows how to test a Web service callout. The implementation of the WebServiceMock interface is listed first. This example implements thedoInvoke 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 Understanding the Generated Code. Import this WSDL and generate a class called docSample before you save this class.
@isTest
global class WebServiceMockImpl implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
       docSample.EchoStringResponse_element respElement = 
           new docSample.EchoStringResponse_element();
       respElement.EchoStringResult = 'Mock response';
       response.put('response_x', respElement); 
   }
}
This is the method that makes a Web service callout.
public class WebSvcCallout {
    public static String callEchoString(String input) {
        docSample.DocSamplePort sample = new docSample.DocSamplePort();
        sample.endpoint_x = 'http://api.salesforce.com/foo/bar';
        
        // This invokes the EchoString method in the generated class 
    
        String echo = sample.EchoString(input);
        
        return echo;
    }   
}
This is the test class containing 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.
@isTest
private class WebSvcCalloutTest {
    @isTest static void testEchoString() {              
        // This causes a fake response to be generated 
    
        Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        
        // Call the method that invokes a callout 
    
        String output = WebSvcCallout.callEchoString('Hello World!');
        
        // Verify that a fake result is returned 
    
        System.assertEquals('Mock response', output); 
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.