+ Start a Discussion
David Durant 5David Durant 5 

Web Services Call Out Help

I have a test class and trigger that I have completed. Currently, it is giving me an error on the web services callout. I tried to do some research to understand why it was giving me this error. I know that the piclist field that I am referencing has a web service callout on the lead object but I am refereing to it on the opportunity.  I also do not know where or how to even set up the mock callout to allow myself to obtain code coverage. Here is my code:
@isTest
public class TestUtilityUpdate{
    static testMethod void myTest(){
        Account acc= new Account(name='Test - Properties');
        insert acc;
        
        set<String> strNames = new set<String>{'Los Angeles Department of Water and Power',
            'Adams Ranch',
            'Amarillo Mutual',
            'Anaheim, City of',
            'Arcadia, City of'
            };

        List<Opportunity> lstOpp = new List<Opportunity>();

        for(String str : strNames)
        {
           Opportunity opp = new Opportunity(Water_Agency__c=str,name=str,closedate=system.today(),stagename='Consult to be Scheduled',Accountid=acc.id);
            lstOpp.add(opp);
        }

insert lstOpp;
    
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi David,

I hope from some trigger your are doing any HTTP Callout. That is why your test class is failing.
You Can create Mock class like below code :-
 
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(200);
        return res;
    }
}

You Can use same mock class in your test class like below class :-
@isTest
private class CalloutClassTest {
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = CalloutClass.getInfoFromExternalService();
        
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

Please refer below blog for more details:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm


And you can use Test.isRunningTest() Method to stop the call out from trigger in case test class is runing
See more detail below :-
https://help.salesforce.com/apex/HTViewSolution?id=000138850&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000138850&language=en_US)

Please let us know if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
David Durant 5David Durant 5
Since I didnt build the web service I am not even sure how to create a fake response. 

Where do I start? I saw this example in the developer book but I dont understand how to take my class and impliment the this mock class. 
JayChenJayChen

Hi David! I'm running into the same issue on writing the test coverage for a mock callout, did you ever get a response for your question on how to implement the mock call?