Skip to content

Instantly share code, notes, and snippets.

@tarot
Created February 1, 2016 16:28
Show Gist options
  • Save tarot/390947a9886c25ab3e03 to your computer and use it in GitHub Desktop.
Save tarot/390947a9886c25ab3e03 to your computer and use it in GitHub Desktop.
QueueableとCalloutとTest
public class CalloutJob implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
String endpoint = 'https://trust.salesforce.com/';
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(endpoint);
new Http().send(req);
}
}
@IsTest
private class CalloutJobTest {
public class CalloutMock implements HttpCalloutMock {
public HttpResponse respond(HttpRequest req) {
return new HttpResponse();
}
}
@IsTest
static void testRunAsAdminUser() {
Test.setMock(HttpCalloutMock.class, new CalloutMock());
Test.startTest();
// runAsの有無は関係ない
System.runAs([select Id from User where Id = :UserInfo.getUserId()][0]) {
System.enqueueJob(new CalloutJob());
}
// System.CalloutException: Callout loop not allowed
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment