Salesforce Decode
Salesforcedecode
Back to questions
ApexIntermediatetestingcalloutsintegration

Write Apex tests for callout retry logic with multi-mock HttpCalloutMock

Real World Scenario

Callout utility retries three times on 503. Tests only mock single success missing retry and exhaustion paths.

Expected Answer

• Custom HttpCalloutMock returning sequence: 503, 503, 200 • Assert retry count and final success state in test • Separate test: three 503s assert exception and no DML side effects • Test.setMock before Test.startTest for Queueable callout jobs • Verify idempotency: retry does not duplicate external POST • Mock timeout with CalloutException path • Assert callout request headers include auth and idempotency key

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Main difference: use case and scale. Custom HttpCalloutMock returning sequence: 503, 503, 200. Assert retry count and final success state in test. Pick based on your integration pattern and team capability. Callout tests must cover retry exhaustion—not only happy path single response. Balance speed of delivery with maintainability.

Architect Perspective

Callout tests must cover retry exhaustion—not only happy path single response.