+ Start a Discussion
Harjeet Singh 13Harjeet Singh 13 

How to cover codes for WSDL2 generated class

Hi All,

I have to cover test class for a WDL2 generated class. However I wrote a test class and succeed in achieving 70% but not able to cover further.
Below is the WSDL2 generated class:
public class SendPickupRequestInfoToTranSend1 {
    public class pickupRequest {
        public SendPickupRequestInfoToTranSend1.PickupRequestData PickupRequestData;
        public SendPickupRequestInfoToTranSend1.DGData[] DGData;
        private String[] PickupRequestData_type_info = new String[]{'PickupRequestData','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] DGData_type_info = new String[]{'DGData','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','-1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'PickupRequestData','DGData'};
    }
    public class getPickupRequest {
        public SendPickupRequestInfoToTranSend1.pickupRequest pickupRequestSalesforce;
        private String[] pickupRequestSalesforce_type_info = new String[]{'pickupRequestSalesforce','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'pickupRequestSalesforce'};
    }
    public class getPickupRequestResponse {
        public SendPickupRequestInfoToTranSend1.pickupResponse pickupResponseSalesforce;
        private String[] pickupResponseSalesforce_type_info = new String[]{'pickupResponseSalesforce','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'pickupResponseSalesforce'};
    }
    public class PickupRequestData {
        public String pickupStatus;
        public String businessUnit;
        public String bookingRef;
        public String createdDate;
        public String accountNumber;
        public String accountName;
        public String pickupAddressLine1;
        public String pickupAddressLine2;
        public String pickupAddressState;
        public String pickupAddressPostCode;
        public String pickupAddressSuburb;
        public String pickupDepot;
        public String pickupContactName;
        public String pickupContactPhone;
        public String isItThirdParty;
        public String readyTime;
        public String closeTime;
        public String remarks;
        public String serviceCode;
        public String payType;
        public String noOfItems;
        public String itemDescription;
        public String totalWt;
        public String lengthcm;
        public String widthcm;
        public String heightcm;
        public String destinationPostcode;
        public String destinationState;
        public String destinationSuburb;
        public String deliveryDepot;
        public String dangerousGoods;
        public String pickupDate;
        private String[] pickupStatus_type_info = new String[]{'pickupStatus','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] businessUnit_type_info = new String[]{'businessUnit','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] bookingRef_type_info = new String[]{'bookingRef','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] createdDate_type_info = new String[]{'createdDate','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] accountNumber_type_info = new String[]{'accountNumber','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] accountName_type_info = new String[]{'accountName','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupAddressLine1_type_info = new String[]{'pickupAddressLine1','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupAddressLine2_type_info = new String[]{'pickupAddressLine2','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupAddressState_type_info = new String[]{'pickupAddressState','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupAddressPostCode_type_info = new String[]{'pickupAddressPostCode','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupAddressSuburb_type_info = new String[]{'pickupAddressSuburb','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupDepot_type_info = new String[]{'pickupDepot','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupContactName_type_info = new String[]{'pickupContactName','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupContactPhone_type_info = new String[]{'pickupContactPhone','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] isItThirdParty_type_info = new String[]{'isItThirdParty','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] readyTime_type_info = new String[]{'readyTime','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] closeTime_type_info = new String[]{'closeTime','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] remarks_type_info = new String[]{'remarks','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] serviceCode_type_info = new String[]{'serviceCode','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] payType_type_info = new String[]{'payType','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] noOfItems_type_info = new String[]{'noOfItems','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] itemDescription_type_info = new String[]{'itemDescription','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] totalWt_type_info = new String[]{'totalWt','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] lengthcm_type_info = new String[]{'lengthcm','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] widthcm_type_info = new String[]{'widthcm','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] heightcm_type_info = new String[]{'heightcm','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] destinationPostcode_type_info = new String[]{'destinationPostcode','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] destinationState_type_info = new String[]{'destinationState','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] destinationSuburb_type_info = new String[]{'destinationSuburb','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] deliveryDepot_type_info = new String[]{'deliveryDepot','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] dangerousGoods_type_info = new String[]{'dangerousGoods','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] pickupDate_type_info = new String[]{'pickupDate','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'pickupStatus','businessUnit','bookingRef','createdDate','accountNumber','accountName','pickupAddressLine1','pickupAddressLine2','pickupAddressState','pickupAddressPostCode','pickupAddressSuburb','pickupDepot','pickupContactName','pickupContactPhone','isItThirdParty','readyTime','closeTime','remarks','serviceCode','payType','noOfItems','itemDescription','totalWt','lengthcm','widthcm','heightcm','destinationPostcode','destinationState','destinationSuburb','deliveryDepot','dangerousGoods','pickupDate'};
    }
    public class DGData {
        public String unCode;
        public String unCodeDescription;
        private String[] unCode_type_info = new String[]{'unCode','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] unCodeDescription_type_info = new String[]{'unCodeDescription','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'0','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'unCode','unCodeDescription'};
    }
    public class pickupResponse {
        public String BookingNumber;
        public String ErrorDescription;
        public String ReceivedDateTime;
        public String ResponseCode;
        private String[] BookingNumber_type_info = new String[]{'BookingNumber','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] ErrorDescription_type_info = new String[]{'ErrorDescription','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] ReceivedDateTime_type_info = new String[]{'ReceivedDateTime','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] ResponseCode_type_info = new String[]{'ResponseCode','http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest','false','false'};
        private String[] field_order_type_info = new String[]{'BookingNumber','ErrorDescription','ReceivedDateTime','ResponseCode'};
    }
    public class aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Port {
        public String endpoint_x = 'https://b2b-uat-farm.toll.com.au:7590/ws/aSalesforce.ws.provider.PickupRequest:receivePickupRequest/aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Port';
        //public String endpoint_x = 'https://requestb.in/rahjklra';
        //public String endpoint_x = 'https://b2b-uat-farm.toll.com.au:7590/aSalesforce.ws.provider.PickupRequest:receivePickupRequest/aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Port';
        
        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;
        private String[] ns_map_type_info = new String[]{'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest', 'SendPickupRequestInfoToTranSend1'};
        public SendPickupRequestInfoToTranSend1.pickupResponse getPickupRequest(SendPickupRequestInfoToTranSend1.pickupRequest pickupRequestSalesforce) {
            SendPickupRequestInfoToTranSend1.pickupResponse res =  new SendPickupRequestInfoToTranSend1.pickupResponse();
            try{
            SendPickupRequestInfoToTranSend1.getPickupRequest request_x = new SendPickupRequestInfoToTranSend1.getPickupRequest();
            request_x.pickupRequestSalesforce = pickupRequestSalesforce;
            SendPickupRequestInfoToTranSend1.getPickupRequestResponse response_x;
            Map<String, SendPickupRequestInfoToTranSend1.getPickupRequestResponse> response_map_x = new Map<String, SendPickupRequestInfoToTranSend1.getPickupRequestResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Binder_getPickupRequest',
              'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',
              'getPickupRequest',
              'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',
              'getPickupRequestResponse',
              'SendPickupRequestInfoToTranSend1.getPickupRequestResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.pickupResponseSalesforce;
            }Catch(Exception e){
                return res;
            }
            return res;
        }
    }
}

My test class is follows:
@IsTest
private with sharing class SendPickupRequestInfoToTranSend1_test
{
    
    
    private class WebServiceMockImpl implements WebServiceMock
    {
        public void doInvoke(
            Object stub, Object request, Map<String, Object> response,
            String endpoint, String soapAction, String requestName,
            String responseNS, String responseName, String responseType)
        {
            if(request instanceof SendPickupRequestInfoToTranSend1.getPickupRequest)
                response.put('response_x', new SendPickupRequestInfoToTranSend1.getPickupRequestResponse());
            return;
        }
    }
    private static testMethod void coverTypes()
    {
        new SendPickupRequestInfoToTranSend1.pickupRequest();
		new SendPickupRequestInfoToTranSend1.getPickupRequest();
		new SendPickupRequestInfoToTranSend1.getPickupRequestResponse();
		new SendPickupRequestInfoToTranSend1.PickupRequestData();
		new SendPickupRequestInfoToTranSend1.DGData();
		new SendPickupRequestInfoToTranSend1.pickupResponse();
		new SendPickupRequestInfoToTranSend1.aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Port ();
		new SendPickupRequestInfoToTranSend1.DGData();
	}

I am not able to cover the lines for the method of port class.Please see below lines:
public SendPickupRequestInfoToTranSend1.pickupResponse getPickupRequest(SendPickupRequestInfoToTranSend1.pickupRequest pickupRequestSalesforce) {
            SendPickupRequestInfoToTranSend1.pickupResponse res =  new SendPickupRequestInfoToTranSend1.pickupResponse();
            try{
            SendPickupRequestInfoToTranSend1.getPickupRequest request_x = new SendPickupRequestInfoToTranSend1.getPickupRequest();
            request_x.pickupRequestSalesforce = pickupRequestSalesforce;
            SendPickupRequestInfoToTranSend1.getPickupRequestResponse response_x;
            Map<String, SendPickupRequestInfoToTranSend1.getPickupRequestResponse> response_map_x = new Map<String, SendPickupRequestInfoToTranSend1.getPickupRequestResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'aSalesforce_ws_provider_PickupRequest_receivePickupRequest_Binder_getPickupRequest',
              'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',
              'getPickupRequest',
              'http://10.164.163.3/aSalesforce.ws.provider.PickupRequest:receivePickupRequest',
              'getPickupRequestResponse',
              'SendPickupRequestInfoToTranSend1.getPickupRequestResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.pickupResponseSalesforce;
            }Catch(Exception e){
                return res;
            }
            return res;
        }
Also I am attaching the screenshot from Developer Console:
User-added image

Kindly help me

Thanks & Regards,
Harjeet
 
Akhil MehraAkhil Mehra
You need to create a MockClass and call that mock class in the test class , you got coverage 



You need to write Mock like this which specify the endpoint and need to specify the response when  the endpoint is called.

@IsTest(SeeAllData=true)
public class MockClassGenerator implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req){ 
            HttpResponse response = new HttpResponse();
             if(req.getEndpoint().contains('https://api.twilio.com/2010-04-01/Accounts/AC7e95462c0dee71ef032344e19a722d70/Messages.json'))
             {
         
                    response.setBody('{"sid": "SM41f757ba73bb4085a3db2fa42f173ebe", "date_created": "Mon, 17 Apr 2017 15:57:35 +0000", "date_updated": "Mon, 17 Apr 2017 15:57:35 +0000", "date_sent": null, "account_sid": "AC7e95462c0dee71ef032344e19a722d70", "to": "+16198055452", "from": "+16195667694", "messaging_service_sid": null, "body": "nj,dnznxhzkjhkxc", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": "USD", "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/AC7e95462c0dee71ef032344e19a722d70/Messages/SM41f757ba73bb4085a3db2fa42f173ebe.json", "subresource_uris": {"media": "/2010-04-01/Accounts/AC7e95462c0dee71ef032344e19a722d70/Messages/SM41f757ba73bb4085a3db2fa42f173ebe/Media.json"}}'); 
                   response.setHeader('Content-Type', 'application/json');
                        response.setStatusCode(200);
             }
                else
                {    
                     response.setBody('{"sid": "SM41f757ba73bb4085a3db2fa42f173ebe", "date_created": "Mon, 17 Apr 2017 15:57:35 +0000", "date_updated": "Mon, 17 Apr 2017 15:57:35 +0000", "date_sent": null, "account_sid": "AC7e95462c0dee71ef032344e19a722d70", "to": "+16198055452", "from": "+16195667694", "messaging_service_sid": null, "body": "nj,dnznxhzkjhkxc", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": "USD", "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/AC7e95462c0dee71ef032344e19a722d70/Messages/SM41f757ba73bb4085a3db2fa42f173ebe.json", "subresource_uris": {"media": "/2010-04-01/Accounts/AC7e95462c0dee71ef032344e19a722d70/Messages/SM41f757ba73bb4085a3db2fa42f173ebe/Media.json"}}'); 
                       response.setHeader('Content-Type', 'application/json');
                        response.setStatusCode(400); 
                }
            return response;
            }
        

    }