# How do developers utilize HttpCalloutMock interfaces to test Apex callout logic successfully during unit tests?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Callout Testing, HttpCalloutMock

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developers-utilize-httpcalloutmock-interfaces-to-test-apex-callout-logic-successfully-during-unit-tests

## Answer

**Basics:** Apex tests are not allowed to make real HTTP callouts; without a mock the test fails with "Methods defined as TestMethod do not support Web service callouts". A mock class returns a canned `HttpResponse`, letting you test success, error codes, bad JSON and timeouts. Salesforce requires 75% overall Apex coverage to deploy to production, and callout code cannot be covered without mocks.

**How it works:**
1. Create a class implementing `HttpCalloutMock` with `HttpResponse respond(HttpRequest req)`.
2. In the test, call `Test.setMock(HttpCalloutMock.class, new MyMock());` before invoking the callout.
3. Run the code (inside `Test.startTest()` / `Test.stopTest()` for async code) and assert on results.
4. For SOAP callouts from WSDL2Apex use `WebServiceMock`; for `Continuation` use `Test.setContinuationResponse` and `Test.invokeContinuationMethod`.

**Advanced / deep dive:** One mock can serve multiple endpoints by branching on `req.getEndpoint()` and `req.getMethod()`, or use `MultiStaticResourceCalloutMock` with JSON files in static resources. Capture the request in a static field to assert headers and body. Test error paths: return status 500 or 429, or throw a `CalloutException` from `respond`. For queueable or future callouts, the mock applies when the async job runs at `Test.stopTest()`. To avoid "uncommitted work pending" in tests, create data in `@testSetup` (which commits) rather than DML right before the callout. For stronger design, wrap callouts behind an interface or service class so higher-level tests can inject a stub instead of mocking HTTP. Assertions matter: coverage alone is not quality.

**Code / config example:**
```apex
@IsTest
global class OrderMock implements HttpCalloutMock {
    global HttpResponse respond(HttpRequest req) {
        System.assertEquals('POST', req.getMethod());
        HttpResponse res = new HttpResponse();
        res.setStatusCode(200);
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"status":"ACCEPTED"}');
        return res;
    }
}
@IsTest
static void testCallout() {
    Test.setMock(HttpCalloutMock.class, new OrderMock());
    Test.startTest();
    String status = OrderService.submit('001000000000001');
    Test.stopTest();
    System.assertEquals('ACCEPTED', status);
}
```

**Hidden gotchas & must-remember points:**
- `Test.setMock` must run before the callout.
- Mocks do not simulate latency or timeouts unless you throw an exception.
- Callout after DML in a test hits the same uncommitted-work error.
- Test data setup in `@testSetup` is committed per class.
- `HttpCalloutMock` is not "mandatory framework"; it is the only way to test callouts.

**Best practice:** Cover success, failure and malformed-response paths with assertions, and keep mocks reusable.

---
Source: ForceChallenges · last updated 2026-11-02
