Fluent API
Write clean, readable test mocks with a chainable, intuitive interface that makes your tests easy to understand.
A production-ready library for mocking HTTP callouts in Salesforce with a clean, fluent API
Traditional Salesforce HTTP mocking requires creating verbose mock classes for every test scenario. HTTP Mock Lib simplifies this with a fluent, chainable API:
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());new HttpMock()
.whenGetOn('/api/v1/authorize')
.body('{"example":"test"}')
.statusCodeOk()
.mock();@IsTest
private class CalloutServiceTest {
@IsTest
static void testMultipleEndpoints() {
// Arrange
new HttpMock()
.whenGetOn('/api/v1/authorize')
.body('{ "token": "aZ3Xb7Qk" }')
.statusCodeOk()
.whenPostOn('/api/v1/create')
.body('{ "success": true, "message": null }')
.statusCodeCreated()
.mock();
// Act
Test.startTest();
CalloutResult result = new CalloutService().makeCallout();
Test.stopTest();
// Assert
Assert.isTrue(result.success);
}
}statusCodeOk(), statusCodeNotFound()HTTP Mock Lib is part of Apex Fluently, a suite of production-ready Salesforce libraries by Beyond the Cloud.
Ready to simplify your HTTP mocking? Get started →