+ Start a Discussion
Jake KolasinskiJake Kolasinski 

Test Multiple Http Callouts

Hello,

I am trying to increase my code coverage and I have to  test my http callouts that are located within the controller. I have been able to test http callouts if they are located in their own static method that returns an http response but I have trouble when it is anywhere else.
 
This is the method for the controller that is executed automatically on page load. I set variables equal to the response of the methods that contain the actual http callouts. 
public HTTPResonse M_AccountMatchesLastName_Brighton;
public HTTPResonse M_AccountMatchesLastName_Decatur;

public Mitchell_Account(ApexPages.StandardController controller) {   
        SF_Account =  (Account) controller.getRecord();
        SF_AccountID = ApexPages.currentPage().getParameters().get('id');
       
                
    
            M_AccountMatchesLastName_Brighton = M_SearchContacts(sAuthentication, sAPIKey, sConnectionID, 'test');
            
            M_AccountMatchesLastName_Decatur = M_SearchContacts(sAuthentication, sAPIKey, sConnectionID, 'test');                                                     
}

 I will have to test multiple variables that are equal to the http response of the method that performs the callout.

Here is the method that is called and actually performs the request:
 
public static HttpResponse M_SearchContacts(String authentication, String apiKey, String connectionId, String keyword){
       
        HttpRequest req = new HttpRequest();
       
        req.setEndpoint(sCalloutEndPoint  +''+keyword);
        req.setMethod('GET');
       
        req.setHeader('cache-control', 'no-cache');
        req.setHeader('Accept', 'application/json');
        req.setHeader('authentication', Authentication);
        req.setHeader('apiKey', apiKey);
        req.setHeader('connectionId', connectionID);
 
        Http http = new Http();
        HTTPResponse res = http.send(req);  
        return res;   
    }


 I have been able to successfully test the M_SearchContacts method by itself but I am not able to test it when setting a variable equal to its response.
 
@isTest
public class Test_Mitchell_Account{
   
 
    @isTest
    static void Test_Controller(){

  Account acct = new Account(Name='test', 
                                   Phone='test',
                                   BillingCity='test',
                                   BillingState='test',
                                   BillingStreet='test',
                                   BillingPostalCode='test',
                                   BillingCountry='test',
                                   Mitchell_ID__c='a743b6e1-6b50-4a00-be85-88811b19dd0c',
                                   Mitchell_Details__c='a743b6e1-6b50-4a00-be85-88811b19dd0c');
        insert acct;  
        

        ApexPages.currentPage().getParameters().put('id',acct.id);        
       
       
        ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        Mitchell_Account Instance_Mitchell_Account = new Mitchell_Account(sc);

Test.setMock(HttpCalloutMock.class, new MockController());
       
        HttpResponse response = Instance_Mitchell_Account.M_AccountMatchesLastName_Brighton;
 
       
        String contentType = response.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = response.getBody();
        System.debug(response.getBody());
        String expectedValue ='I have removed the expected value as it is lengthy';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, response.getStatusCode());
 
       
    }


 
 
And I try to use this mock callout class. I have the conditional statement in there since I will be performing multiple http callouts in the controller method above.
 
 
@isTest
global class MockController implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setHeader('cache-control', 'no-cache');
       
                if (req.getEndpoint().startsWith(sCalloutEndPoint)) {
                    HTTPResponse res = new HTTPResponse();
                    res.setBody('I have removed the expected value as it is lengthy');
                    res.setStatusCode(200);
                    return res;
                } else if (req.getEndpoint().startsWith(sCalloutEndPoint) && req.getEndpoint().contains('/Invoice')) {
                    HTTPResponse res = new HTTPResponse();
                    res.setBody('I have removed the expected value as it is lengthy');
                    res.setStatusCode(200);
                    return res;
                } else {
                    System.assert(false, 'unexpected endpoint ' + req.getEndpoint());
                    HTTPResponse res = new HTTPResponse();
                    return res;
                }
    }
   
}


 
I am receiving the error: Methods defined as TestMethod do not support Web service callouts
 
I am not entirely sure if I am trying to test this properly or if I need to take a different approach. Please do not feel obligated to help! But any help is appreciated



Thank you for any sort of help!
Martha VMartha V