• Jake Kolasinski
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
  • 0

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.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.
public class Test_Mitchell_Account{
    static void Test_Controller(){

  Account acct = new Account(Name='test', 
        insert acct;  

        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();
        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.
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');
                    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');
                    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!