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
bs881026bs881026 

How to write a test class for a Web service

Hi,

Can anyone please guide me as to how do we write the test class for a Web Service.

Thanks.
Ajay Nagar 7Ajay Nagar 7
Go through these links :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
https://developer.salesforce.com/forums/?id=906F00000008z4VIAQ
Amit Chaudhary 8Amit Chaudhary 8
Please check below trailhead module for all webservice related qun.
1) https://developer.salesforce.com/trailhead/module/apex_integration_services?utm_campaign=trailhead&utm_source=sfdc&utm_medium=chatter-success

Apex Integration Overview
https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_callouts
This module will Describe the differences between web service and HTTP callouts and Authorize an external site with remote site settings


Apex REST Callouts
https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_rest_callouts
This module will  describe about Perform a callout to receive data from an external service and Test callouts by using mock callouts 

Apex SOAP Callouts
https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_soap_callouts
This module will describe you about Generate Apex classes using WSDL2Apex and  Perform a callout to send data to an external service using SOAP with Test callouts by using mock callouts


Apex Web Services
https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_webservices
This module will Describe the two types of Apex web services and provide a high-level overview of these services. Create an Apex REST class that contains methods for each HTTP method and Invoke a custom Apex REST method with an endpoint

NOTE:- Test class example ith Mock classes also given in above module

Let us know if this will help u

 
Suraj Tripathi 47Suraj Tripathi 47

Hi bs881026,
You can write test class two ways-
  1)By using static resources-In this you need to go 
   Developer Console-> select File -> New -> Static Resource.
  2)By implementing Interface.
   Create class->then implement interface.
Go through this link for more information.
Sample code by using Interface.
@isTest
global class AnimalLocatorMock  implements HttpCalloutMock{
   global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
       response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
    
        response.setStatusCode(200);
        return response; 
    }
}
@isTest
public class AnimalLocatorTest {
    @isTest
    static void test(){
        Test.setMock(HttpCallOutMock.class, new AnimalLocatorMock());
        String res=AnimalLocator.getAnimalNameById(2);
    }

}
https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts#apex_integration_rest_callouts_resources

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi