Skip to content

HTTP Mock Lib

Simplify HTTP callout testing.

API version

License

GitHub Repo starsGitHub ReleaseCodecov

Why Http Mock Lib?

  • Mock different HTTP methods - Mock different HTTP methods in the same test method.
  • Fluent API - Make your mocking English-like, no additional HttpCalloutMock classes required.

Quick Start

❌❌❌

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('{ "token": "aZ3Xb7Qk" }')
        .statusCodeOk()
    .whenPostOn('/api/v1/create')
        .body('{ "success": true, "message": null }')
        .statusCodeOk()
    .mock();

Documentation

Installation

Released under the MIT License.