[SalesForce] Future Callout Unit Tests!

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

Best Answer

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.

Related Topic