0

There are two integration classes. One gets a response from the other. I am not able to call the Mock class from Test class hence I am not able to get mock response and I am not able to increase the code coverage. I checked this link but it's not the issue. I have reduced the code to the bare minimum and inserted comments for directions. I tried my best to describe the issue. Please guide. Thank You!

COPSContractDocumentsViewController This is the primary class that I want to cover

Public class COPSContractDocumentsViewController {
    //some code
    public void getDocContentAsAttachment(){
        //some code
        sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get apDocContent = new sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get();
        sapComDocContentSapSoapFunctionsMcS.ZzApDocContentsGetResponse_element apDocContentResponse = new sapComDocContentSapSoapFunctionsMcS.ZzApDocContentsGetResponse_element();
        //some code
        apDocContentResponse = apDocContent.ZzApDocContentsGet(DocObjkeyValue);//This returns null
        if(apDocContentResponse != null){//not getting value in apDocContentResponse
            //not able to enter here. I want to enter here
        }
    }
}

COPSContractDocumentsViewController_TEST This is the test class of primary class. Its not able to call the mock class

@isTest
public class COPSContractDocumentsViewController_TEST{
    public  static testMethod void testMethod(){
        //some code
        COPSContractDocumentsViewController docInstance = new COPSContractDocumentsViewController();
        test.startTest();
        Test.setMock(WebServiceMock.class, new sapComDocContentSapSoapFunctionsMcSMock()); //This mock class is not getting called
        sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get apDocContent = new sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get();
        docInstance.getDocContentAsAttachment(); 
        test.stopTest();
    }
}

sapComDocContentSapSoapFunctionsMcS This is the supplementary class used in primary class. When I run the test class of primary class, all lines except last 2 lines are covered. I am not able to understand why the last two lines are not covered.

public class sapComDocContentSapSoapFunctionsMcS {
    public class ZzApDocContentsGet_element {
        public String IvObjKey;
        private String[] IvObjKey_type_info = new String[]{'//some code
        private String[] apex_schema_type_info = new String[]{'//some code
        private String[] field_order_type_info = new String[]{'//some code
    }
    public class ZzApDocContentsGetResponse_element {
        public String EvMimeType;
        public String EvStream;
        private String[] EvMimeType_type_info = new String[]{//some code
        private String[] EvStream_type_info = new String[]{//some code
        private String[] apex_schema_type_info = new String[]{//some code
        private String[] field_order_type_info = new String[]{//some code
    }
    public class zz_ap_doc_contents_get {
        //some code
        public sapComDocContentSapSoapFunctionsMcS.ZzApDocContentsGetResponse_element ZzApDocContentsGet(String IvObjKey) {
            //some code
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              //some code
            );
            response_x = response_map_x.get('response_x');//This line is not covered
            return response_x;//This line is not covered
        }
    }
}

sapComDocContentSapSoapFunctionsMcSMock This is the mock class of the supplementary class. This class is not getting executed.

@isTest
global class sapComDocContentSapSoapFunctionsMcSMock 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) {

           sapComDocContentSapSoapFunctionsMcS.ZzApDocContentsGetResponse_element respElement  = new sapComDocContentSapSoapFunctionsMcS.ZzApDocContentsGetResponse_element();   
           respElement.EvMimeType = 'Mock response EvMimeType';
           respElement.EvStream = 'Mock response EvStream';
           response.put('response_x', respElement);  
   }
}

sapComDocContentSapSoapFunctionsMcSTest This is the test class of supplementary class and it gives 100% coverage for the supplementary class. This one is able to call the mock class.

@isTest
private class sapComDocContentSapSoapFunctionsMcSTest {
    @isTest static void testSapComDocContentSapSoapFunctionsMcS() {   
        Test.setMock(WebServiceMock.class, new sapComDocContentSapSoapFunctionsMcSMock());
        sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get obj = new sapComDocContentSapSoapFunctionsMcS.zz_ap_doc_contents_get();
        obj.ZzApDocContentsGet('Hello World!');
    }
}
4
  • Just for curiosity, can you remove the Start and Stop test methods and run it or move the setMock before starttest method
    – manjit5190
    Apr 2, 2020 at 14:36
  • @manjit5190 Thanks for your time! I tried it. It didn't work.
    – Koustubh
    Apr 2, 2020 at 14:48
  • Did you get a fix for this?
    – manjit5190
    Apr 4, 2020 at 14:21
  • @manjit5190 no, I didn't get a fix for it.
    – Koustubh
    Apr 5, 2020 at 15:08

0

You must log in to answer this question.

Browse other questions tagged .