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.
- Host: GitHub
- URL: https://github.com/beyond-the-cloud-dev/http-mock-lib
- Owner: beyond-the-cloud-dev
- License: mit
- Created: 2024-10-04T09:11:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-03-12T21:23:24.000Z (4 months ago)
- Last Synced: 2026-03-13T03:45:29.599Z (4 months ago)
- Topics: apex, apex-fluently, apex-mocking
- Language: Apex
- Homepage: https://httpmock.beyondthecloud.dev/
- Size: 1.65 MB
- Stars: 36
- Watchers: 1
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# 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)