Status Codes
Set HTTP status codes for your mocked responses.
Built-in Status Codes
HTTP Mock Lib provides semantic methods for common HTTP status codes.
Success Codes (2xx)
statusCodeOk()
HttpMock statusCodeOk() // 200Standard success response.
new HttpMock()
.whenGetOn('/api/users')
.body('{"users": []}')
.statusCodeOk()
.mock();statusCodeCreated()
HttpMock statusCodeCreated() // 201Resource successfully created.
new HttpMock()
.whenPostOn('/api/users')
.body('{"id": "123"}')
.statusCodeCreated()
.mock();statusCodeAccepted()
HttpMock statusCodeAccepted() // 202Request accepted for processing.
new HttpMock()
.whenPostOn('/api/jobs')
.body('{"jobId": "456", "status": "pending"}')
.statusCodeAccepted()
.mock();statusCodeNoContent()
HttpMock statusCodeNoContent() // 204Success with no response body.
new HttpMock()
.whenDeleteOn('/api/users/123')
.statusCodeNoContent()
.mock();Client Error Codes (4xx)
statusCodeBadRequest()
HttpMock statusCodeBadRequest() // 400Invalid request.
new HttpMock()
.whenPostOn('/api/users')
.body('{"error": "Invalid email format"}')
.statusCodeBadRequest()
.mock();statusCodeUnauthorized()
HttpMock statusCodeUnauthorized() // 401Authentication required.
new HttpMock()
.whenGetOn('/api/users')
.body('{"error": "Authentication required"}')
.statusCodeUnauthorized()
.mock();statusCodeForbidden()
HttpMock statusCodeForbidden() // 403Access denied.
new HttpMock()
.whenGetOn('/api/admin')
.body('{"error": "Access denied"}')
.statusCodeForbidden()
.mock();statusCodeNotFound()
HttpMock statusCodeNotFound() // 404Resource not found.
new HttpMock()
.whenGetOn('/api/users/999')
.body('{"error": "User not found"}')
.statusCodeNotFound()
.mock();statusCodeMethodNotAllowed()
HttpMock statusCodeMethodNotAllowed() // 405HTTP method not supported.
new HttpMock()
.whenPatchOn('/api/immutable-resource')
.body('{"error": "PATCH not allowed"}')
.statusCodeMethodNotAllowed()
.mock();Server Error Codes (5xx)
statusCodeInternalServerError()
HttpMock statusCodeInternalServerError() // 500Server error.
new HttpMock()
.whenGetOn('/api/users')
.body('{"error": "Internal server error"}')
.statusCodeInternalServerError()
.mock();statusCodeNotImplemented()
HttpMock statusCodeNotImplemented() // 501Functionality not implemented.
new HttpMock()
.whenPostOn('/api/v2/feature')
.body('{"error": "Not implemented"}')
.statusCodeNotImplemented()
.mock();statusCodeBadGateway()
HttpMock statusCodeBadGateway() // 502Invalid response from upstream server.
new HttpMock()
.whenGetOn('/api/external')
.body('{"error": "Bad gateway"}')
.statusCodeBadGateway()
.mock();statusCodeServiceUnavailable()
HttpMock statusCodeServiceUnavailable() // 503Service temporarily unavailable.
new HttpMock()
.whenGetOn('/api/users')
.body('{"error": "Service unavailable"}')
.statusCodeServiceUnavailable()
.mock();statusCodeGatewayTimeout()
HttpMock statusCodeGatewayTimeout() // 504Gateway timeout.
new HttpMock()
.whenGetOn('/api/slow-endpoint')
.body('{"error": "Gateway timeout"}')
.statusCodeGatewayTimeout()
.mock();Custom Status Codes
For status codes not covered by built-in methods, use statusCode():
HttpMock statusCode(Integer statusCode)Example:
new HttpMock()
.whenGetOn('/api/users')
.body('{"error": "Too many requests"}')
.statusCode(429) // Custom: Too Many Requests
.mock();Default Status Code
If no status code is specified, HTTP Mock Lib uses 200 (OK) by default:
// These are equivalent:
new HttpMock()
.whenGetOn('/api/users')
.body('{"users": []}')
.mock();
new HttpMock()
.whenGetOn('/api/users')
.body('{"users": []}')
.statusCodeOk() // Explicitly set
.mock();Testing Error Handling
Use error status codes to test how your code handles failures:
@IsTest
static void testUnauthorizedError() {
// Arrange
new HttpMock()
.whenGetOn('/api/secure-data')
.body('{"error": "Unauthorized"}')
.statusCodeUnauthorized()
.mock();
// Act & Assert
Test.startTest();
try {
new ApiService().getSecureData();
Assert.fail('Expected CalloutException');
} catch (CalloutException e) {
Assert.isTrue(e.getMessage().contains('Unauthorized'));
}
Test.stopTest();
}Complete Status Code Reference
| Code | Method | Description |
|---|---|---|
| 200 | statusCodeOk() | Success |
| 201 | statusCodeCreated() | Resource created |
| 202 | statusCodeAccepted() | Request accepted |
| 204 | statusCodeNoContent() | Success, no content |
| 400 | statusCodeBadRequest() | Bad request |
| 401 | statusCodeUnauthorized() | Unauthorized |
| 403 | statusCodeForbidden() | Forbidden |
| 404 | statusCodeNotFound() | Not found |
| 405 | statusCodeMethodNotAllowed() | Method not allowed |
| 500 | statusCodeInternalServerError() | Server error |
| 501 | statusCodeNotImplemented() | Not implemented |
| 502 | statusCodeBadGateway() | Bad gateway |
| 503 | statusCodeServiceUnavailable() | Service unavailable |
| 504 | statusCodeGatewayTimeout() | Gateway timeout |
| Custom | statusCode(Integer) | Any status code |
Best Practices
Use Semantic Methods - Prefer
statusCodeOk()overstatusCode(200)for readabilityTest Error Paths - Don't just test success cases; mock error responses too
Match Real APIs - Use status codes that match what the real API returns
Document Exceptions - When testing error cases, document why you expect them
