Created
March 28, 2017 09:33
-
-
Save Sunil02kumar/be364948aa495c4a6df3b1ddd48f55fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalloutUtility { | |
//method to retrieve information about the Salesforce version | |
public static HttpResponse performCallout() { | |
HttpRequest req = new HttpRequest(); | |
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); | |
req.setHeader('Content-Type', 'application/json'); | |
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm(); | |
system.debug('********domainUrl:'+domainUrl); | |
req.setEndpoint(domainUrl+'/services/data/'); | |
req.setMethod('GET'); | |
Http h = new Http(); | |
HttpResponse res = h.send(req); | |
system.debug(res.getBody()); | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class CalloutUtilityTest1 { | |
testMethod static void unittest1() { | |
StaticResourceCalloutMock mockCallout = new StaticResourceCalloutMock(); | |
mockCallout.setStaticResource('StaticResourceForCallout'); | |
mockCallout.setStatusCode(200); | |
mockCallout.setHeader('Content-Type', 'application/json'); | |
// Set the mock callout mode | |
Test.setMock(HttpCalloutMock.class, mockCallout); | |
// Call the method that performs the callout | |
HTTPResponse res = CalloutUtility.performCallout(); | |
System.assertEquals(200,res.getStatusCode()); | |
System.assertEquals('application/json', res.getHeader('Content-Type')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class CalloutUtilityTest2 { | |
testMethod static void testCallout() { | |
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new HTTPMockCallout()); | |
// from the class that implements HttpCalloutMock. | |
HttpResponse res = CalloutUtility.performCallout(); | |
// Verify response received contains fake values | |
String contentType = res.getHeader('Content-Type'); | |
System.assert(contentType == 'application/json'); | |
System.assertEquals(200, res.getStatusCode()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
global class HTTPMockCallout implements HttpCalloutMock { | |
global HTTPResponse respond(HTTPRequest req) { | |
HttpResponse res = new HttpResponse(); | |
res.setHeader('Content-Type', 'application/json'); | |
string jsonResBody = '{ "label": "Spring \'17", "url": "/services/data/v39.0","version": "39.0"}'; | |
res.setBody(jsonResBody); | |
res.setStatusCode(200); | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment