1

I am testing a SOAP callout. The callout happens on a after insert and after update. The issue that I am facing is that, when I insert or update a record in the unit tests, I can trace that it reaches the method to do the callout but it does not execute the callout, hence the main code is not tested.

Helper Class: This is a synchronous method called below from a async method which makes it async. Reason is the sync. call is used in the batch processes.

public static void Sync_ProcessCallOut(RCMServiceStubSchema.ContactMaintDetails_element[] arrMappedReferrals){ 

         RCMServiceStub.ContactMaintForSalesforceEndpoint1 stub = new RCMServiceStub.ContactMaintForSalesforceEndpoint1();
         RCMServiceStubSchema.Status_element responseStatus = new RCMServiceStubSchema.Status_element();
         Map <ID, String> MapIDStatus = new Map <ID, String>();
         map <ID, String> mapIDOperation = new Map <ID, String>();
         String ChannelInd = 'SJ';    

         try{   
                 System.Debug('This is the request' + arrMappedReferrals);
                 if(arrMappedReferrals != null && !arrMappedReferrals.isEmpty()){
                    System.Debug('Calling the service now');

                    RCMServiceStub.ContactMaintForSalesforceEndpoint1 stubEndpoint = RCMUtility.getRCMService();   
                    responseStatus = stubEndpoint.ContactMaintForSalesforceOP(ChannelInd, arrMappedReferrals);

                    System.Debug('This is the response'+ responseStatus);

                     if(responseStatus.StatusDesc == 'Success'){
                        System.Debug('Reaching here');

                        // CHECK IF THE REQUEST WAS SUCCESSFUL      
                        // Update referral Status to sent for each referral
                        for(RCMServiceStubSchema.ContactMaintDetails_element mappedReferral : arrMappedReferrals){
                            System.Debug('Sent');
                            MapIDStatus.put(mappedReferral.SalesforceID, 'Sent');
                        }
                    }
                    else
                        System.Debug('Request sent but not received');
                 }

                 // Update the Integration Status to Sent
                 if(MapIDStatus.keySet() != null && !MapIDStatus.keySet().isEmpty()){
                   Sync_updateReferralStatusOperation(mapIDStatus, mapIDOperation);   
                 }
         }
         catch(Exception e){
            SystemLogger.Log(e);
            SystemLogger.flushLogCache();
         }
      }

Test Class:

Test.startTest();

       System.runAs(u1){ 
        Contact contac = UnitTestHelper.reusableCont();
        contac.firstName = 'Test';
        contac.lastName = 'User';
        insert Contac;

        Referral__c ref = [Select Id, referred_to_contact__c from Referral__c where product__c = 'Corporate banking'];
        ref.referred_to_Contact__c = contac.Id;

        mapIDRefAfter.put(ref.Id, ref);
        SalesforceRCMReferralSync.compareValuesOnUpdate(mapIDRefBefore, mapIDRefAfter);

      } 
   Test.stopTest(); 

So, everything works fine. The compareValuesOnUpdate method checks for the values and calls the asynchronous method. It starts to make the callout but the code just never gets coverage for some odd reason. Am I doing something wrong?

Screenshot: enter image description here

  • subodh Same issue here. Lets wait for experts good advice... – Bforce Apr 9 '14 at 18:17
  • If you don't mind being a beta tester, the FuseIT SFDC Explorer will now generate a basic WSDL Mock that can be used with Test.setMock for a SOAP based web service. – Daniel Ballinger Apr 9 '14 at 21:11
  • I shall definitely give that a shot Daniel! THanks. – subodhbahl Apr 10 '14 at 2:25
6

Asynchronous calls are executed after Test.stopTest. You can validate execution after calling Test.stopTest. Also, you need to set up a mock receiver class to simulate the call out. See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm for details on this.

  • Thanks sfdcfox, I figured out the issue. I should maybe write a blogpost on this! – subodhbahl Apr 10 '14 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.