{"id":869,"date":"2015-02-02T05:10:10","date_gmt":"2015-02-02T05:10:10","guid":{"rendered":"http:\/\/www.greytrix.com\/blogs\/salesforce\/?p=869"},"modified":"2018-10-08T05:47:28","modified_gmt":"2018-10-08T05:47:28","slug":"testing-web-service-callouts","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/02\/02\/testing-web-service-callouts\/","title":{"rendered":"Testing Web Service Callouts"},"content":{"rendered":"<p>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.<\/p>\n<p><strong><strong><a title=\"Salesforce.com integration with Sage 100 (MAS 90\/200) ERP \u2013 An introduction\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/01\/21\/salesforce-com-integration-with-sage-100-mas-90200-erp-an-introduction\/\" target=\"_blank\" rel=\"noopener\"><strong>Real-time, bi-directional, multi-company &#8211; Sage ERP Integration with Salesforce<\/strong><\/a><br \/>\n<\/strong><\/strong><\/p>\n<p>While writing the test coverage we came to know that by default, test methods don\u2019t 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.<\/p>\n<p>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.<\/p>\n<p>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-<\/p>\n<ol>\n<li>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.\u00a0<a href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout1.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-870\" src=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout1.png\" alt=\"callout1\" width=\"823\" height=\"325\" \/><\/a><\/li>\n<li>Following is the method that callouts the Web service.<a href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout2.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-871\" src=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout2.png\" alt=\"callout2\" width=\"576\" height=\"223\" \/><\/a><\/li>\n<li>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.<br \/>\n<a href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout3.png\"><img decoding=\"async\" class=\"size-full wp-image-872 aligncenter\" src=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2015\/02\/callout3.png\" alt=\"callout3\" width=\"565\" height=\"256\" \/><\/a><\/li>\n<\/ol>\n<p><span style=\"font-size: Medium;\"><strong>About Us<\/strong><\/span><br \/>\n<a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a>\u00a0as 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.<\/p>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a>\u00a0has some unique solutions for Cloud CRM such as Salesforce integration with\u00a0<a href=\"https:\/\/www.greytrix.com\/product\/sage-x3\/gumu-x3-salesforce-integration\">Sage Enterprise Management (Sage X3<\/a>),\u00a0Sage Intacct, <a href=\"https:\/\/www.greytrix.com\/product\/sage-100-erp\/gumu-100-salesforce-integration\">Sage 100\u00a0<\/a>and\u00a0<a href=\"https:\/\/www.greytrix.com\/product\/sage-300-erp\/gumu-300-salesforce-integration\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM\u00a0<a href=\"https:\/\/www.greytrix.com\/product\/crm-development\/salesforce-com\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/data-migration-services\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/integrated-application-development\">Integrated App development<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/custom-development\">Custom App development<\/a> and <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/maintenance-support\">Technical Support<\/a> to business partners and end users.<\/p>\n<p>Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star app listed on <a href=\"https:\/\/appexchange.salesforce.com\/listingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<\/p>\n<p>For more information, please contact us at\u00a0<a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n<p><strong><span style=\"font-size: Medium;\">Related Posts<\/span><\/strong><\/p>\n<ul>\n<li><a title=\"Hiding non removable fields from a page layout\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/02\/02\/hiding-non-removable-fields-from-a-page-layout\/\" target=\"_blank\" rel=\"noopener\">Hiding non removable fields from a page layout\u00a0<\/a><\/li>\n<li><a title=\"GUMU Sage 100 ERP Integration (Lite) now on Salesforce AppExchange\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/01\/29\/gumu-sage-100-erp-integration-lite-now-on-salesforce-app-store\/\" target=\"_blank\" rel=\"noopener\">GUMU Sage 100 ERP Integration (Lite) now on Salesforce AppExchange<\/a><\/li>\n<li><a title=\"Salesforce.com integration with Sage 100 (MAS 90\/200) ERP \u2013 An introduction\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/01\/21\/salesforce-com-integration-with-sage-100-mas-90200-erp-an-introduction\/\" target=\"_blank\" rel=\"noopener\">Salesforce.com integration with Sage 100 (MAS 90\/200) ERP \u2013 An introduction<\/a><\/li>\n<li><a title=\"Adding Google Map to an address in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/01\/02\/adding-google-map-to-an-address-in-salesforce\/\" target=\"_blank\" rel=\"noopener\">Adding Google Map to an address in Salesforce<\/a><\/li>\n<li><a title=\"Delete all records from a Custom Object\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2014\/12\/31\/delete-all-records-from-a-custom-object\/\" target=\"_blank\" rel=\"noopener\">Delete all records from a Custom Object<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8211; Sage\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2015\/02\/02\/testing-web-service-callouts\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[488,539,548],"class_list":["post-869","post","type-post","status-publish","format-standard","hentry","category-gumu-salesforce","tag-test-apex","tag-webservice-callout","tag-wsdl"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":4,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":4261,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/869\/revisions\/4261"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}