HTTP Mock Lib
Simplify HTTP callout testing.
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
HttpCalloutMockclasses 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();