TestMethod로 정의된 메서드는 웹 서비스 콜아웃을 지원하지 않습니다.
오류가 발생한 예
@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;
}
}