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
newbiewithapexnewbiewithapex 

How to write test class for Webservice Callout?

I have this main class which uses webservice callout that I am not familiar with and I need to write test class for that. Can someone please help me figure out this one?
public BectranWebService.bectranServiceResult createCustomer(BectranWebService.bectranServiceRequest arg0) {
    BectranWebService.createCustomer request_x = new BectranWebService.createCustomer();
    request_x.arg0 = arg0;
    BectranWebService.createCustomerResponse response_x = new BectranWebService.createCustomerResponse();
    Map<String, BectranWebService.createCustomerResponse> response_map_x = new Map<String, BectranWebService.createCustomerResponse>();
    response_map_x.put('response_x', response_x);
    WebServiceCallout.invoke(
      this,
      request_x,
      response_map_x,
      new String[]{endpoint_x,
      '',
      'http://ws.services.bectran.com/',
      'createCustomer',
      'http://ws.services.bectran.com/',
      'createCustomerResponse',
      'BectranWebService.createCustomerResponse'}
    );
    response_x = response_map_x.get('response_x');
    return response_x.return_x;
}
public BectranWebService.additionalInfoBean getAdditionalAttributesForCreditApp(BectranWebService.loginInfoBean arg0,String arg1,String arg2) {
    BectranWebService.getAdditionalAttributesForCreditApp request_x = new BectranWebService.getAdditionalAttributesForCreditApp();
    request_x.arg0 = arg0;
    request_x.arg1 = arg1;
    request_x.arg2 = arg2;
    BectranWebService.getAdditionalAttributesForCreditAppResponse response_x = new BectranWebService.getAdditionalAttributesForCreditAppResponse();
    Map<String, BectranWebService.getAdditionalAttributesForCreditAppResponse> response_map_x = new Map<String, BectranWebService.getAdditionalAttributesForCreditAppResponse>();
    response_map_x.put('response_x', response_x);
    WebServiceCallout.invoke(this,
                             request_x,
                             response_map_x,
                             new String[]{endpoint_x,
                                          '',
                                          'http://ws.services.bectran.com/',
                                          'getAdditionalAttributesForCreditApp',
                                          'http://ws.services.bectran.com/',
                                          'getAdditionalAttributesForCreditAppResponse',
                                          'BectranWebService.getAdditionalAttributesForCreditAppResponse'
                                          });
    response_x = response_map_x.get('response_x');
    return response_x.return_x;
    }

 
Raj VakatiRaj Vakati
You need to use the mock Webservice .. please refer this link 




https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Deepali KulshresthaDeepali Kulshrestha
Hi,

If you want to create test classes for web services callout.
Please refer to these links it will helps you write test classes for web callouts.
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts
https://eltoro.secure.force.com/TestCoverageForSoapAndRestWebserviceCallouts    
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

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