Created
February 1, 2016 16:28
-
-
Save tarot/390947a9886c25ab3e03 to your computer and use it in GitHub Desktop.
QueueableとCalloutとTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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