reeeval
2/12/2018 - 10:24 AM

Callout Test Class

/*callout class*/
public class CalloutClass {
    public static HttpResponse getInfoFromExternalService(Id myCaseId) {
        Case myCase = [SELECT Id, Account.Integration_Key__c FROM Case WHERE Id = :myCaseId LIMIT 1]; 
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://api.salesforce.com/foo/bar?id=' + myCase.Account.Integration_Key__c);
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        return res;
    }
}

/*HttpCalloutMock class*/
@isTest
global class ExampleCalloutMock implements HttpCalloutMock{
  global HttpResponse respond(HTTPRequest req){
    HttpResponse res = new HttpResponse();
    res.setStatus('OK');
    res.setStatusCode(200);
    res.setBody('GREAT SCOTT');
    return res;
  }
}

/*TestClass class*/
@isTest(seeAllData=False)
private class ExampleCallout_Test{

  static Case getTestCase(){
    Case myCase = new Case(Subject = 'Test');
    insert myCase;
    return myCase;
  }

  static testMethod void shouldBeAbleToGetData(){
    Case myCase = getTestCase();
    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
    HttpResponse res = CalloutClass.getInfoFromExternalService(myCase.Id);
    Test.stopTest();
  }
}