2

written a test class to test the callout that is using SOAP. I have generated class using WSDL. The response line in the generated class not covered though i tried several ways. Given below is the generated class. Since it is not returning a response the code coverage is very low which is 29%. What should i return as a fake response to the test mock class? I have added a comment to the response line in the code below. Any suggestions please..

//Generated by wsdl2apex

public class sapComDocumentSapSoapFunctionsMcS {
    public class ZGssmwfmHndlEvntrqst00_element {
        public String Dpi2Rtrvlastsaveddata;
        public sapComDocumentSapSoapFunctionsMcS.ZgssmbstDatarcrd01 DpistInpt;
        private String[] Dpi2Rtrvlastsaveddata_type_info = new String[]{'Dpi2Rtrvlastsaveddata','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','1','false'};
        private String[] DpistInpt_type_info = new String[]{'DpistInpt','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'Dpi2Rtrvlastsaveddata','DpistInpt'};
    }
    public class Z_GSSMWFM_HNDL_EVNTRQST00 {
        //public String endpoint_x = 'http://testde1:8050/sap/bc/srt/rfc/sap/z_gssmwfm_hndl_evntrqst00/110/z_gssmwfm_hndl_evntrqst00/z_gssmwfm_hndl_evntrqst00';
        public String endpoint_x = 'http://xx.xx.xxx.xx:8050/sap/bc/srt/rfc/sap/z_gssmwfm_hndl_evntrqst00/110/z_gssmwfm_hndl_evntrqst00/z_gssmwfm_hndl_evntrqst00';
        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[]{'urn:sap-com:document:sap:soap:functions:mc-style', 'sapComDocumentSapSoapFunctionsMcS', 'urn:sap-com:document:sap:rfc:functions', 'sapComDocumentSapRfcFunctions'};
        public sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element ZGssmwfmHndlEvntrqst00(String Dpi2Rtrvlastsaveddata,sapComDocumentSapSoapFunctionsMcS.ZgssmbstDatarcrd01 DpistInpt) {
            sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00_element request_x = new sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00_element();
            sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element response_x;
            request_x.Dpi2Rtrvlastsaveddata = Dpi2Rtrvlastsaveddata;
            request_x.DpistInpt = DpistInpt;
            Map<String, sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element> response_map_x = new Map<String, sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'urn:sap-com:document:sap:soap:functions:mc-style',
              'ZGssmwfmHndlEvntrqst00',
              'urn:sap-com:document:sap:soap:functions:mc-style',
              'ZGssmwfmHndlEvntrqst00Response',
              'sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element'}
            );
            response_x = response_map_x.get('response_x'); //this line not covered showing in red color
            return response_x; //this line not covered showing in red color
        }
    }
    public class Bapiret2 {
        public String Type_x;
        public String Id;
        public String Number_x;
        public String Message;
        public String LogNo;
        public String LogMsgNo;
        public String MessageV1;
        public String MessageV2;
        public String MessageV3;
        public String MessageV4;
        public String Parameter;
        public Integer Row;
        public String Field;
        public String System_x;
        private String[] Type_x_type_info = new String[]{'Type','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Id_type_info = new String[]{'Id','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Number_x_type_info = new String[]{'Number','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Message_type_info = new String[]{'Message','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] LogNo_type_info = new String[]{'LogNo','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] LogMsgNo_type_info = new String[]{'LogMsgNo','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] MessageV1_type_info = new String[]{'MessageV1','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] MessageV2_type_info = new String[]{'MessageV2','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] MessageV3_type_info = new String[]{'MessageV3','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] MessageV4_type_info = new String[]{'MessageV4','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Parameter_type_info = new String[]{'Parameter','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Row_type_info = new String[]{'Row','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] Field_type_info = new String[]{'Field','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] System_x_type_info = new String[]{'System','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'Type_x','Id','Number_x','Message','LogNo','LogMsgNo','MessageV1','MessageV2','MessageV3','MessageV4','Parameter','Row','Field','System_x'};
    }
    public class ZgssmbssDatarcrd01 {
        public String Cdata;
        private String[] Cdata_type_info = new String[]{'Cdata','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'Cdata'};
    }
    public class Bapiret2T {
        public sapComDocumentSapSoapFunctionsMcS.Bapiret2[] item;
        private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'item'};
    }
    public class ZgssmbstDatarcrd01 {
        public sapComDocumentSapSoapFunctionsMcS.ZgssmbssDatarcrd01[] item;
        private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'item'};
    }
    public class ZGssmwfmHndlEvntrqst00Response_element {
        public sapComDocumentSapSoapFunctionsMcS.Bapiret2T DpostMssg;
        public sapComDocumentSapSoapFunctionsMcS.ZgssmbstDatarcrd01 DpostOtpt;
        private String[] DpostMssg_type_info = new String[]{'DpostMssg','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] DpostOtpt_type_info = new String[]{'DpostOtpt','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
        private String[] field_order_type_info = new String[]{'DpostMssg','DpostOtpt'};
    }
}

2 Answers 2

1

At a minimum:

public class MyMock 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) {

        sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element r
                = new sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element();
        response.put('response_x', r); 
   }
}
0

In your HttpCalloutMock class, simply construct the expected Response elements. Your WSDL 2 Apex code contains lots of getters / setters, so if you construct the response element and any of the nested inner classes, you should reach close to 100% code coverage quickly.

Additionally as an FYI, if you have any exception handling, you can simply create a second HttpCalloutMock class that throws an exception to test any exception paths as well.

1
  • Take a look at this blog post by Andrew Fawcett for some nice pointers about generating those responses using the instanceof syntax - really is a big timesaver (andyinthecloud.com/2013/05/11/…) Aug 6, 2014 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .