Testing HTTP Callouts using SFCraft MockServer: Part 1

public class CalloutClass {
public static HttpResponse getInfoFromExternalService() {
HttpRequest req = new HttpRequest();
req.setEndpoint('http://example.com/example/test');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
return res;
}
}

Salesforce Example of HttpCalloutMock

@isTest
public class MockHttpResponseGenerator implements HttpCalloutMock {
// Implement this interface method
public HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('http://example.com/example/test', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
@isTest
private class CalloutClassTest {
@isTest
private 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 = '{"example":"test"}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, res.getStatusCode());
}
}

Mocking responses with MockServer

@isTest
private class CalloutClassTest {
private static void configureMockServer() {
// Create api resource
// Resources are responsible for providing correct mock responses for a method requested
sfcraft_MockAPIResource apiResource = new sfcraft_MockAPIResource();
// add a response
apiResource.setResponse(
// method to respond to. Basically this can be any HTTP method (or any string at all!)
'GET',
// code to respond with
200,
// response body (optional, empty string by default)
'{"example":"test"}',
// response headers (optional)
new Map<String, String> {
'Content-Type' => 'application/json'
}
);
// Instantiating a server
sfcraft_MockServer server = new sfcraft_MockServer();
// Setting server as mock
server.setAsMock();
// Adding resource to server and pointing it to a specific endpoint
server.addEndpoint('http://example.com/example/test', apiResource);
}
}
@isTest
private class CalloutClassTest {
private static void configureMockServer() {...
}
@isTest
private static void testCallout() {
// Instead of setting mock callout class, we just call the configuration method
configureMockServer();

// 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 = '{"example":"test"}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, res.getStatusCode());
}
}
private static void configureMockServer() {
// Original Resource
sfcraft_MockAPIResource apiResource = new sfcraft_MockAPIResource();
apiResource.setResponse(
'GET',
200,
'{"example":"test"}',
new Map<String, String> {
'Content-Type' => 'application/json'
}
);
apiResource.setResponse(
// Let's add POST method
'POST',
200,
'{"example":"test"}',
new Map<String, String> {
'Content-Type' => 'application/json'
}
);
apiResource.setResponse(
'GET',
// Let's add another code!
400,
'{"error":"true"}',
new Map<String, String> {
'Content-Type' => 'application/json'
}
);
// let's create another response
sfcraft_MockAPIResource anotherApiResource = new sfcraft_MockAPIResource();
anotherApiResource.setResponse(
'GET',
200,
'{"anotherExample":"test"}',
new Map<String, String> {
'Content-Type' => 'application/json'
}
);
// Instantiating a server
sfcraft_MockServer server = new sfcraft_MockServer();
// Setting server as mock
server.setAsMock();
// Adding resource to server
server.addEndpoint('http://example.com/example/test', apiResource);
server.addEndpoint('http://example.com/anotherExample/test', anotherApiResource);
}

--

--

--

Salesforce Craftsman | Moscow Salesforce User Group Leader | https://www.salesforcecraft.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What I am learning during the transition from a mobile developer to a cloud developer

Introduction to Data Frames in R

Meetup Note: Platform thinking — Creating agility at scale

Stop Applying Makeup on Reality

Azure Percept— Capturing fish dimensions

Golang arm64 docker image with CircleCI arm machine

ScriptX and the World Wide Web: “Link Globally, Interact Locally” (1995)

Career day at Drybar💛

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nikita Chursin

Nikita Chursin

Salesforce Craftsman | Moscow Salesforce User Group Leader | https://www.salesforcecraft.dev

More from Medium

Last but not least — Test Execution Analysis — Tips to do it better.

Selenium Grid 4 setup and usage

Automated Testing with Robot Framework: Here Are The Things You Need To Know

Selenium Element Interactions: Frames and Windows