Testing Web Service Callouts

By | February 2, 2015

Earlier we had generated code from WSDL as an Apex class containing the methods that can be invoked for calling the Web service. As we are aware that to deploy or package this Apex class, 75% of the code must have test coverage, including the methods in the generated class.

Real-time, bi-directional, multi-company – Sage ERP Integration with Salesforce

While writing the test coverage we came to know that by default, test methods don’t support Web service callouts and tests that perform Web service callouts are skipped. To avoid tests from being skipped Apex provides the built-in WebServiceMock interface and the Test.setMock method that one can use to receive fake responses in a test method.

When we generate an Apex class from a WSDL, the methods call WebServiceCallout.invoke to perform the callout to the external web service. Now while testing we need to instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called.

For this, we implement the WebServiceMock interface and specify a fake response that the Apex runtime should send. Following is a simple example that will give an idea on how we can test the web service callouts-

  1. Create the class which implements WebServiceMock interface. This implements the doInvoke method, which returns the response we want. In this case, the response element of the auto-generated class is created and assigned a value. callout1
  2. Following is the method that callouts the Web service.callout2
  3. Below is the test class which tests web service callout in the mock callout mode. It calls the callEchoString method of above class and verifies that a mock response is received.
    callout3

About Us
Greytrix as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce integration with Sage Enterprise Management (Sage X3), Sage Intacct, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.

Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star app listed on Salesforce AppExchange.

For more information, please contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts