+ 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:
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);

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 :-
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');
        return res;

You Can use same mock class in your test class like below class :-
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:-

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

Amit Chaudhary
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. 

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?