QueueableHandler for HttpCalloutMock class throws "Callout loop not allowed" error when Test classes are run
Last updated 2022-02-10 ·Reference W-2701771 ·Reported By 21 users
Summary
When testing a callout with HttpCalloutMock class from a Queueable Apex instance there is a "Callout loop not allowed" exception thrown and captured in the debug logs.
Repro
Create a class that perform a remote callout:
public class PirateTranslator {
public static String translate(String sourceText) {
String endpoint ='http://isithackday.com/arrpi.php?format=text&text=' + EncodingUtil.urlEncode( sourceText,'UTF-8');
HttpRequest req = new HttpRequest();
req.setBody('');
req.setEndpoint(endpoint);
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
return res.GetBody();
}
}
Create a “caller” class that will call this function from a queueable Apex call:
public class TranslateCaller implements queueable, Database.allowsCallouts{
public void execute(QueueableContext context) {
system.debug(PirateTranslator.translate('Hello Friend'));
}
}
Now, create a class to mock the call:
public class MockTranslate implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req)
{
String requestBody = req.getBody();
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'text');
res.setBody(requestBody + ' mocked result');
res.setStatusCode(200);
return res;
}
}
Create the following unit test:
@istest
public class TestTranslate {
public static testmethod void testQueueable() {
Test.setMock(HttpCalloutMock.class, new MockTranslate());
TranslateCaller t = new TranslateCaller();
Test.startTest();
system.enqueueJob(t);
Test.stopTest();
}
}
Workaround
None at this time
Reported By (21)








Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.