Skip to content

HTTP Mock LibFluent HTTP Mocking for Apex

A production-ready library for mocking HTTP callouts in Salesforce with a clean, fluent API

Why HTTP Mock Lib?

Traditional Salesforce HTTP mocking requires creating verbose mock classes for every test scenario. HTTP Mock Lib simplifies this with a fluent, chainable API:

apex
@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());
apex
new HttpMock()
  .whenGetOn('/api/v1/authorize')
  .body('{"example":"test"}')
  .statusCodeOk()
  .mock();

Quick Example

apex
@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);
  }
}

Features at a Glance

  • Mock Multiple Endpoints - Handle GET, POST, PUT, PATCH, DELETE, HEAD, TRACE in one test
  • Flexible Response Bodies - Return String, Object, or Blob responses
  • Built-in Status Codes - Use semantic methods like statusCodeOk(), statusCodeNotFound()
  • Content Type Support - JSON, XML, CSV, PDF, and custom content types
  • Custom Headers - Add any headers your callout needs
  • Zero Configuration - Works out of the box, no setup required

Part of Apex Fluently

HTTP Mock Lib is part of Apex Fluently, a suite of production-ready Salesforce libraries by Beyond the Cloud.

Get Started

Ready to simplify your HTTP mocking? Get started →