An open API service indexing awesome lists of open source software.

https://github.com/beyond-the-cloud-dev/http-mock-lib

Simpler Apex Http Callouts mocking.
https://github.com/beyond-the-cloud-dev/http-mock-lib

apex apex-fluently apex-mocking

Last synced: 3 months ago
JSON representation

Simpler Apex Http Callouts mocking.

Awesome Lists containing this project

README

          





HTTP Mock Lib logo


HTTP Mock Lib

Beyond The Cloud logo

API version
License
GitHub Repo stars
GitHub Release

# Getting Started

HTTP Mock inspired by Robert Sösemann’s [Apex Http Mock](https://github.com/rsoesemann/apex-httpmock).

HTTP Mock Lib is part of [Apex Fluently](https://apexfluently.beyondthecloud.dev/), a suite of production-ready Salesforce libraries by Beyond the Cloud.

❌❌❌

```java
@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());
```

✅✅✅

```java
new HttpMock()
.whenGetOn('/api/v1/authorize').body('{"example":"test"}').statusCodeOk()
.mock();
```

## Example

```java
@IsTest
private class MyTest {
@IsTest
static void calloutTest() {
new HttpMock()
.whenGetOn('/api/v1/authorize').body('{ "token": "aZ3Xb7Qk" }').statusCodeOk()
.whenPostOn('/api/v1/create').body('{ "success": true, "message": null }').statusCodeOk()
.mock();

Test.startTest();
new CalloutService().makeCallout();
Test.stopTest();

// Asserts
Assert.areEqual(..., ...);
}
}
```

## API

```java
public interface HttpMockLib {
HttpMock whenGetOn(String endpointToMock);
HttpMock whenPostOn(String endpointToMock);
HttpMock whenPutOn(String endpointToMock);
HttpMock whenPatchOn(String endpointToMock);
HttpMock whenDeleteOn(String endpointToMock);
HttpMock whenTraceOn(String endpointToMock);
HttpMock whenHeadOn(String endpointToMock);
// Body
HttpMock body(Object body);
HttpMock body(String body);
HttpMock body(Blob body);
// Content-Type
HttpMock contentTypePlainText(); // text/plain
HttpMock contentTypeHtml(); // text/html
HttpMock contentTypeCsv(); // text/csv
HttpMock contentTypeJson(); // application/json
HttpMock contentTypeXml(); // application/xml
HttpMock contentTypePdf(); // application/pdf
HttpMock contentTypeFormUrlencoded(); // application/x-www-form-urlencoded
HttpMock contentType(String contentType);
// Status Code
HttpMock statusCodeOk(); // 200
HttpMock statusCodeCreated(); // 201
HttpMock statusCodeAccepted(); // 202
HttpMock statusCodeNoContent(); // 204
HttpMock statusCodeBadRequest(); // 400
HttpMock statusCodeUnauthorized(); // 401
HttpMock statusCodeForbidden(); // 403
HttpMock statusCodeNotFound(); // 404
HttpMock statusCodeMethodNotAllowed(); // 405
HttpMock statusCodeInternalServerError(); // 500
HttpMock statusCodeNotImplemented(); // 501
HttpMock statusCodeBadGateway(); // 502
HttpMock statusCodeServiceUnavailable(); // 503
HttpMock statusCodeGatewayTimeout(); // 504
HttpMock statusCode(Integer statusCode);
// Headers
HttpMock header(String key, String value);
// Mock
void mock();
}
```

## Documentation

Visit the [documentation](https://httpmock.beyondthecloud.dev/) to view the full documentation.

## Contributors



## License notes:

- For proper license management each repository should contain LICENSE file similar to this one.
- each original class should contain copyright mark: © Copyright 2025, Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)