1

I've a service class that makes a callout to specific endpoint inside a future method. The callout is made on after update trigger on Contact when a relevant field update is done.

 @future(callout=true)
    public static void sendContactUpdate(Set<Id> contactIds) {
        List<Contact> contactsToUpdate = new List<Contact>();
        List<Contact> contactsToSend   = new CL_ContactSelector().getContactsById(contactIds);

    
    for (Contact contact : contactsToSend) {
            CL_IntegrationCalloutResponse response = (CL_IntegrationCalloutResponse) 
                                                        new CL_CDEContactCalloutService(contact.CL_CLAASID__c)
                                                        .call(new Map<String, Object>{'contactToSend' => contact});

            System.debug('response ->'+ response);

            if (response.status == 0) { 
                contactsToUpdate.add(contact);
                CL_Utility.createCalloutLogEntry(getLogEntryParams(SUCCESS, new CL_UserUpdateRequest(contact), response));
            } else {
                CL_Utility.createCalloutLogEntry(getLogEntryParams(FAIL, new CL_UserUpdateRequest(contact), response));
            }
    }

    updateContacts(contactsToUpdate);
}

the callout works as expected. And, i'm trying to write test for this method by mocking the response using the HttpCalloutMock interface. This is my test class :

 /* Integration test */
@IsTest
static void CL_CDEContactCalloutServiceTest() { 
    List<Contact> customerContacts = createContacts();
    for (Contact customerContact : customerContacts) {
        customerContact.Email = '[email protected]'; //changing a relevant field for user Update call
    }

    Test.startTest();
        Test.setMock(HttpCalloutMock.class, new CL_UserUpdateCalloutMock());
        update customerContacts;
    Test.stopTest();

    List<Contact> contactsAfterUpdate = [SELECT Id, CL_IncorrectAddress__c FROM Contact WHERE Id = :customerContacts];

    Assert.isTrue(!contactsAfterUpdate.isEmpty());
    for (Contact contactAfterUpdate : contactsAfterUpdate) {
        Assert.areEqual(false, contactAfterUpdate.CL_IncorrectAddress__c);
    }
}

This is how my Mock class:

@IsTest
global class CL_UserUpdateCalloutMock implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest request) {
        request.setHeader('Content-Type', 'application/json');
        String jsonBody = [SELECT Body FROM StaticResource WHERE Name = 'UserUpdateCalloutMock'].Body.toString();
    
        HttpResponse response = new HttpResponse(); 
        response.setBody(jsonBody);
        response.setStatusCode(200);
        return response; 
    }
}

Yes, the jsonBody query returns a value. However, my mock is always returning null

response -> null

My mock implementation is as per the documentation. Not sure where i'm going wrong. Any pointers will be helpful.

3
  • what does the .call method look like?
    – cropredy
    Commented Sep 7, 2023 at 17:21
  • @cropredy its the method that makes the callout to the end and returns the response Commented Sep 7, 2023 at 19:21
  • if your debug line shows response -> null then I'd say the call method isn't doing any callout. Are all of its input conditions satisfied?
    – cropredy
    Commented Sep 8, 2023 at 0:42

0

You must log in to answer this question.

Browse other questions tagged .