Quantcast
Sunday , August 19 2018
Home / Apex / How To Do In Salesforce / How to make HTTP Callouts in salesforce and how to test HTTP Callouts?

How to make HTTP Callouts in salesforce and how to test HTTP Callouts?

How to make HTTP Callouts in salesforce and how to test HTTP Callouts?

Salesforce has created three classes to perfom HTTP Callouts namely, Http, HttpRequest, HttpResponse classes.

How to make Http Callout n Salesforce

Http: to initiate the Request and response of, HttpRequest class creates a request object for methods like GET,POST etc and HttpResponse class creates a response object returned from the callout.

Before making Http callout we need to understand that, the response of web callout from the system is dependent on the external web server is therefore advised that web callout should be performed asynchronously, i.e. use @future Annotation, otherwise no further processing before we receive a response.

In below example we will make a GET Http Request to an external web server

Public class HTTPExample {

Public String getHttpResponseFromWebServer(string url){

Http http = new Http();

HttpRequest  req = new HttpRequest();

req.setEndpoint(url);

req.setMethod(‘GET’);

HttpResponse res = http.send(req);

Return res.getBody();

}

}

This is a very basic example of making Http Callout.

Creating Http Callout is easy task, but to deploy code to production we need to cover 75% of the code lines. But test method do not support Http Callouts, then how do we test a class making Http Callout. To overcome this Salesforce has provided with a trick where we provide the test class with a mock response and we use these response to verify the callout class.

To create a class which generates a mock Http callout reponse has to implement HttpCalloutMock Interface. E.g.

Global class mockClassName implements HttpCallMock{

Global HTTPResponse respond(HttpRequest req){

//Write your logic here to create a mock / fake response

}

}

Once we have created our mock callout class we need to call it during the execution of test class. For this we have to call a method Test.setMock and its syntax is as following.

Test.setMock(HttpCalloutMock.class, new mockClassName ());

After this method is called in test class, if an Http request is invoked then no callout is made but the response generated from your mock class’s method is used

Below is a full example of Http callout and how to test it.

@isTest

global class yourMockHttpResponseGeneratorClass implements HttpCalloutMock {

global HTTPResponse respond(HTTPRequest req) {

System.assertEquals(‘http://abc.com/abc’, req.getEndpoint());

System.assertEquals(‘GET’, req.getMethod());

HttpResponse res = new HttpResponse();

res.setHeader(‘Content-Type’, ‘application/json’);

res.setBody(‘{“example”:”test”}’);

res.setStatusCode(200);

return res;

}

}

 

public class YouClassToCallout {

public static HttpResponse getInfoFromWebService() {

HttpRequest req = new HttpRequest();

req.setEndpoint(‘http://abc.com/abc’);

req.setMethod(‘GET’);

Http http = new Http();

HttpResponse res = http.send(req);

return res;

}

}

@isTest

private class YouClassToCalloutTest {

@isTest static void testingTheCallout() {

// Set your mock response callout class here

Test.setMock(HttpCalloutMock.class, new yourMockHttpResponseGeneratorClass ());

HttpResponse res = YouClassToCallout.getInfoFromWebService ();

 

// you can Verify the fake response received

String contentType = res.getHeader(‘Content-Type’);

System.assert(contentType == ‘application/json’);

String actualValue = res.getBody();

String expectedValue = ‘{“example”:”test”}’;

System.assertEquals(actualValue, expectedValue);

System.assertEquals(200, res.getStatusCode());

}

}

 

This is how you make a Http Callout and test it to deploy to production.

 

Also, Have a look at the below resources:

  1. Best Salesforce Interview Questions book with Apex and Visualforce concept explained

Also, Have a look at the below learning resources:

  1. SOQL (Salesforce Object Query Language)

  2. Apex Trigger Best Practices and the Trigger Framework

  3. Salesforce Interview Question and Answers Part 2

  4. Salesforce Interview Questions on Test Class

  5. Salesforce-lightning-interview-questions-2018

     6. Salesforce Interview Questions Batch Class 

About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also

MODAL Box

How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …

Salesforce Lightning Tutorial

How to fetch and display list of records in Lightning Component

How to fetch and display list of records in Lightning Component? In this post we …

Leave a Reply

Your email address will not be published. Required fields are marked *