anusha(salesforce developer)

Monday, 1 August 2016

Test Web Service Callouts

Test 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 fail. To prevent tests from failing and to increase code coverage, Apex provides the built-in WebServiceMockinterface and the Test.setMock method. Use WebServiceMock and Test.setMock to receive fake responses in a test method.

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.
01global class YourWebServiceMockImpl implements WebServiceMock {
02   global void doInvoke(
03           Object stub,
04           Object request,
05           Map<String, Object> response,
06           String endpoint,
07           String soapAction,
08           String requestName,
09           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}
Note
  • The class implementing the WebServiceMock interface can be either global or public.
  • You can annotate this class with @isTest because it is used only in a test context. In this way, you can exclude it from your org’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:
1Test.setMock(WebServiceMock.classnew 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.

Note
To mock a callout if the code that performs the callout is in a managed package, call Test.setMock from a test method in the same package with the same namespace.
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.
01@isTest
02global class WebServiceMockImpl implements WebServiceMock {
03   global void doInvoke(
04           Object stub,
05           Object request,
06           Map<String, Object> response,
07           String endpoint,
08           String soapAction,
09           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.
01public class WebSvcCallout {
02    public static String callEchoString(String input) {
03        docSample.DocSamplePort sample = new docSample.DocSamplePort();
04        sample.endpoint_x = 'http://api.salesforce.com/foo/bar';
05         
06        // This invokes the EchoString method in the generated class
07        String echo = sample.EchoString(input);
08         
09        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.
01@isTest
02private class WebSvcCalloutTest {
03    @isTest static void testEchoString() {             
04        // This causes a fake response to be generated
05        Test.setMock(WebServiceMock.classnew WebServiceMockImpl());
06         
07        // Call the method that invokes a callout
08        String output = WebSvcCallout.callEchoString('Hello World!');
09         
10        // Verify that a fake result is returned
11        System.assertEquals('Mock response', output);
12    }
13}

No comments:

Post a Comment