TestMethod として定義されたメソッドは、Web サービスコールアウトをサポートしません。
エラー発生した例
@isTest
private class PostTest {
@isTest
static void test() {
Test.startTest();
Service.execute();
Test.stopTest();
}
}
public with sharing class Service {
@future(callout=true)
public static void execute() {
ServiceC sc = new ServiceC();
sc.post();
}
}
public with sharing class ServiceC {
private HttpResponse post() {
HttpRequest request = new HttpRequest();
(省略)
HttpResponse response = new Http().send(request);
return response;
}
}
エラー解消例
@isTest
private class PostTest {
@isTest
static void test() {
Test.startTest();
Test.setMock(HttpCalloutMock.class, new ServiceHttpCalloutMock());
Service.execute();
Test.stopTest();
}
}
@isTest
public class ServiceHttpCalloutMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(200);
res.setBody('test');
return res;
}
}