Last active
August 21, 2020 02:10
-
-
Save tarot/43430fd7192d9dffd248 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 CalloutSampleTest { | |
public class CalloutMock implements HttpCalloutMock { | |
public HttpResponse respond(HttpRequest req) { | |
return new HttpResponse(); | |
} | |
} | |
@IsTest | |
static void test() { | |
System.runAs([select Id from User where Id = :UserInfo.getUserId()][0]) { | |
String name = EncodingUtil.convertToHex(Crypto.generateAesKey(128)); | |
insert new User( | |
LastName = name, | |
Username = name + '@example.com', | |
Email = name + '@example.com', | |
ProfileId = UserInfo.getProfileId(), | |
Alias = name.left(8), | |
TimeZoneSidKey = 'Asia/Tokyo', | |
LocaleSidKey = 'ja_JP', | |
EmailEncodingKey = 'UTF-8', | |
LanguageLocaleKey = 'ja' | |
); | |
} | |
Test.startTest(); | |
Test.setMock(HttpCalloutMock.class, new CalloutMock()); | |
HttpRequest req = new HttpRequest(); | |
req.setMethod('GET'); | |
req.setEndpoint('https://login.salesforce.com'); | |
new Http().send(req); | |
Test.stopTest(); | |
} | |
} |
insertするのがUserじゃない場合は(少なくともAccount, Contact, カスタムオブジェクトでは)ならない。
insertしなくてももちろんならない。
ちなみにTest.setMock
しない時の例外メッセージはこれ
TestMethod として定義されたメソッドは、Web サービスコールアウトをサポートしないため、テストはスキップされます。
The Test.startTest statement must appear before the Test.setMock statement.
と書いてあるけど順番が逆でも挙動は変わらない様子。
コミュニティを有効化していた時に発生するバグだったらしい。cs5が悪いのではなかった。
修正されました
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
何故かcs5でこうなる。ap2ではならない。