Last active
August 29, 2015 14:02
-
-
Save jeriley/2d23be2be117cf7c89c7 to your computer and use it in GitHub Desktop.
Salesforce Apex http callout test helper and example
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 FullMultiMock implements HttpCalloutMock { | |
private integer urlCount { get; set; } | |
private List<MockCallout> mockCallouts {get; set;} | |
public FullMultiMock(List<MockCallout> callouts){ | |
system.debug('callouts : ' + callouts); | |
mockCallouts = callouts; | |
urlCount = 0; | |
} | |
public HTTPResponse respond(HTTPRequest request){ | |
HTTPResponse response = new HTTPResponse(); | |
response.setHeader('Content-Type', 'application/json'); //this could be changed to a variable per call | |
MockCallout callout = mockCallouts[urlCount]; | |
system.assert(callout.HttpMethod == request.getMethod(), 'HttpMethod mismatch on #' + (urlCount + 1) + ', was expecting ' + request.getMethod()); | |
if (callout.HttpMethod == 'POST') | |
System.Assert(callout.PayloadBody == request.getBody(), 'callout number : ' + urlCount + ' bad post data -- expected :' + callout.PayloadBody + ' actual : ' + request.getBody()); | |
System.Assert(callout.ExpectedUrl == request.getEndpoint(), 'callout number : ' + urlCount + ' actual endpoint : ' + request.getEndpoint() + ' expected : ' + callout.ExpectedUrl); | |
response.setBody(callout.ReturnBody); | |
response.setStatusCode(callout.ReturnStatusCode); | |
urlCount++; | |
return response; | |
} | |
public void AssertAllCallsMade(){ | |
System.assert(urlCount == mockCallouts.size(), 'not all expected callouts were made. Expected : ' + mockCallouts.size() + ' actual : ' + urlCount); | |
} | |
} |
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 MockCallout { | |
public MockCallout(String returnBody, String expectedUrl, String payloadBody, String httpMethod, Integer returnStatusCode){ | |
this.ReturnBody = returnBody; | |
this.ExpectedUrl = expectedUrl; | |
this.PayloadBody = payloadBody; | |
this.ReturnStatusCode = returnStatusCode; | |
this.HttpMethod = httpMethod; | |
} | |
public String ReturnBody {get;set;} | |
public String ExpectedUrl {get;set;} | |
public String PayloadBody {get;set;} | |
public String HttpMethod {get;set;} | |
public Integer ReturnStatusCode {get;set;} | |
} |
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 | |
public class TestUseExample { | |
static testMethod void ExampleUse(){ | |
MockCallout getJson = new MockCallout('{ "data": "json return data" }', //return data | |
'/user/100' //url expected | |
'' //payload data | |
'GET', 200) //verb and response code | |
MockCallout updateUser = new MockCallout('{ "data": "updated json" }', | |
'/user/100', | |
'{ "name" : "json" }', | |
'POST', 200); | |
FullMultiMock mocks = new FullMultiMock(new MockCallout[]{ getJson, updateUser }); | |
Test.setMock(HttpCalloutMock.class, mocks); | |
//some httprequest/response/do stuff code gets called here | |
//test class assert | |
mocks.AssertAllCallsMade(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment