Tuesday, 28 July 2015

Test class for Webservice Callout

Webservice class:
-----------------
globalclass RecordCreationservicecls {
       webservicestatic String createRecord(String jsonString){
              //Process to create a record
              return'';
       }
}



Generated class for the above web service class “RecordCreationservicecls”

//Generated by wsdl2apex
publicclass GeneratedRecordCreationService {
publicclass LogInfo {
public String category;
public String level;
private String[] category_type_info = new String[]{'category','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] level_type_info = new String[]{'level','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'category','level'};
    }
publicclass AllowFieldTruncationHeader_element {
public Boolean allowFieldTruncation;
private String[] allowFieldTruncation_type_info = new String[]{'allowFieldTruncation','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'allowFieldTruncation'};
    }
publicclass DebuggingHeader_element {
public GeneratedRecordCreationService.LogInfo[] categories;
public String debugLevel;
private String[] categories_type_info = new String[]{'categories','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'0','-1','false'};
private String[] debugLevel_type_info = new String[]{'debugLevel','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    }
publicclass CallOptions_element {
public String client;
private String[] client_type_info = new String[]{'client','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'client'};
    }
publicclass location {
public Double latitude;
public Double longitude;
private String[] latitude_type_info = new String[]{'latitude','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] longitude_type_info = new String[]{'longitude','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'latitude','longitude'};
    }
publicclass address {
public String city;
public String country;
public String countryCode;
public String postalCode;
public String state;
public String stateCode;
public String street;
private String[] city_type_info = new String[]{'city','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] country_type_info = new String[]{'country','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] countryCode_type_info = new String[]{'countryCode','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] postalCode_type_info = new String[]{'postalCode','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] state_type_info = new String[]{'state','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] stateCode_type_info = new String[]{'stateCode','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] street_type_info = new String[]{'street','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'city','country','countryCode','postalCode','state','stateCode','street'};
    }
publicclass createRecordResponse_element {
public String result;
private String[] result_type_info = new String[]{'result','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','true'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'result'};
    }
publicclass createRecord_element {
public String jsonString;
private String[] jsonString_type_info = new String[]{'jsonString','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','true'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'jsonString'};
    }
publicclass SessionHeader_element {
public String sessionId;
private String[] sessionId_type_info = new String[]{'sessionId','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'sessionId'};
    }
publicclass DebuggingInfo_element {
public String debugLog;
private String[] debugLog_type_info = new String[]{'debugLog','http://soap.sforce.com/schemas/class/RecordCreationservicecls',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls','true','false'};
private String[] field_order_type_info = new String[]{'debugLog'};
    }
publicclass RecordCreationservicecls {
public String endpoint_x = 'https://ap2.salesforce.com/services/Soap/class/RecordCreationservicecls';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
public GeneratedRecordCreationService.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
public GeneratedRecordCreationService.CallOptions_element CallOptions;
public GeneratedRecordCreationService.SessionHeader_element SessionHeader;
public GeneratedRecordCreationService.DebuggingInfo_element DebuggingInfo;
public GeneratedRecordCreationService.DebuggingHeader_element DebuggingHeader;
private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=http://soap.sforce.com/schemas/class/RecordCreationservicecls';
private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/schemas/class/RecordCreationservicecls';
private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/schemas/class/RecordCreationservicecls';
private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/schemas/class/RecordCreationservicecls';
private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/schemas/class/RecordCreationservicecls';
private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/schemas/class/RecordCreationservicecls', 'GeneratedRecordCreationService'};
public String createRecord(String jsonString) {
            GeneratedRecordCreationService.createRecord_element request_x = new GeneratedRecordCreationService.createRecord_element();
            request_x.jsonString = jsonString;
            GeneratedRecordCreationService.createRecordResponse_element response_x;
            Map<String, GeneratedRecordCreationService.createRecordResponse_element> response_map_x = new Map<String, GeneratedRecordCreationService.createRecordResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
this,
              request_x,
              response_map_x,
new String[]{endpoint_x,
'',
'http://soap.sforce.com/schemas/class/RecordCreationservicecls',
'createRecord',
'http://soap.sforce.com/schemas/class/RecordCreationservicecls',
'createRecordResponse',
'GeneratedRecordCreationService.createRecordResponse_element'}
            );
            response_x = response_map_x.get('response_x');
return response_x.result;
        }
    }
}

Create Service Mockup:
Sample Mockup class:
Create Mockupclass:
------------------------
global class YourWebServiceMockImpl implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {

        // Create response element from the autogenerated class.
        // Populate response element.
        // Add response element to the response parameter, as follows:
        response.put('response_x', responseElement);
   }
}

Original Mockup class:
globalclass GeneratedRecordCreationServiceMockupcls implements WebServiceMock {
       globalvoid doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {

// Create response element from the autogenerated class.
        GeneratedRecordCreationService.createRecordResponse_element responseElement = new GeneratedRecordCreationService.createRecordResponse_element();
//Here createRecordResponse_element is an inner class in generated service class for preparing response
// Populate response element.
        responseElement.result = 'Welcome to Salesfoce';//here we have to set the value for the property result
// Add response element to the response parameter, as follows:
        response.put('response_x', responseElement);
   }
      
}


Test class:
@isTest
privateclass TestGeneratedRecordCreationService {

statictestMethodvoid myUnitTest() {
// TO DO: implement unit test
 Test.setMock(WebServiceMock.class, new GeneratedRecordCreationServiceMockupcls());

        GeneratedRecordCreationService.RecordCreationservicecls recordCreationObj = new  GeneratedRecordCreationService.RecordCreationservicecls();
       recordCreationObj.createRecord('Give input');
      //This method “createRecord” contains the webservice callout.
    }
}

So by using mockup class we can do test code coverage for the method which includes web service callout .

References: