function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

Test Class for Webservice Tutorial

Hello,

I have developed a webservice, i want to write a test class for the same. 
Can somone guide me on the test class ?

Thank you in advance 
Best Answer chosen by Ab
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Test methods don’t support Web service callouts and tests that perform Web service callouts are skipped. To prevent the test from being skipped and to increase code coverage, Apex provides the built-in WebServiceMock interface and the Test.setMock method that you can use to receive fake responses in a test method.

WebServiceMock interface enables sending a fake response when testing web service callouts of a class auto-generated from WSDL.

doInvoke is the method of this interface.

doInvoke(stub, soapRequest, responseMap, endpoint, soapAction, requestName, responseNameSpace, responseName, responseType)

The implementation of this method is called by the Apex Runtime to send a fake response when a service callout is made after Test.setMock has been called.

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

https://howtodoitinsalesforce.blogspot.com/2018/08/Test-Class-HTTPCallouts-in-Salesforce.html

Please check below trailhead modules. Test class example with Mock classes also given in the above modules.

Apex Web Services: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_webservices

Apex REST Callouts: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

Apex SOAP Callouts: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_soap_callouts

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Test methods don’t support Web service callouts and tests that perform Web service callouts are skipped. To prevent the test from being skipped and to increase code coverage, Apex provides the built-in WebServiceMock interface and the Test.setMock method that you can use to receive fake responses in a test method.

WebServiceMock interface enables sending a fake response when testing web service callouts of a class auto-generated from WSDL.

doInvoke is the method of this interface.

doInvoke(stub, soapRequest, responseMap, endpoint, soapAction, requestName, responseNameSpace, responseName, responseType)

The implementation of this method is called by the Apex Runtime to send a fake response when a service callout is made after Test.setMock has been called.

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

https://howtodoitinsalesforce.blogspot.com/2018/08/Test-Class-HTTPCallouts-in-Salesforce.html

Please check below trailhead modules. Test class example with Mock classes also given in the above modules.

Apex Web Services: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_webservices

Apex REST Callouts: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

Apex SOAP Callouts: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_soap_callouts

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Sandrine,

Please refer to these links it may be helpful to you.
https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_callouts
https://www.salesforcetutorial.com/salesforce-integration-testing/
https://www.greytrix.com/blogs/salesforce/2015/02/02/testing-web-service-callouts/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
https://developer.salesforce.com/forums/?id=906F0000000kExOIAU


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha