如何从第一个 class 中的第二个 class func 调用测试 http 调用(APEX class testing/salesforce 测试)
How to test a http calls from a second class func call from within first class (APEX class testing/salesforce testing)
我有一个顶点 class A。我从 A 中调用 B class 的函数。
class A{
B bobj=new B();
B.function2();
function1(); //within class A
}
这个函数 1 和函数 2 是对 2 个 diff rest 服务的 http 调用。
我用 httpcallmockouts 为 function1 和 function2 写了一个测试 class。所以 APex Test class-
中的顺序是这样的
B objB=new B();
HttpResponse res = CalloutB.getInfoFromExternalService();
A objA=new A();
HttpResponse res = CalloutA.getInfoFromExternalService();
//Till this point my test runs successfully for http calls mock
Test.startTest();
objA.function1();
//Here I get an error http callout not supported for test methods for function2 of B class.
//If I change the order above to
//A objA=new A();
//HttpResponse res = CalloutA.getInfoFromExternalService();
//B objB=new B();
//HttpResponse res = CalloutB.getInfoFromExternalService();
//Then I get error http callout not supported for test methods for function1 of A class.
Test.stopTest();
那么,如何在第一个 class.
中测试来自另一个 class 函数调用的 http 调用
发生这种情况是因为您没有初始化 httpMock class 对象,因此您的测试 class 正在尝试访问测试 class 不允许的真实 Web 服务调用。你需要做些什么来初始化你的 mockClass 对象并使用它来代替 return 数据而不是访问真正的网络服务端点
In main class which does the callout declare a variable which holds
reference to created mockClass object
Add a condition in your callout class which check if its a test class which is currently running using Test.isRunningTest() .
If its a test class then use your mock class to return data instead of real callout
If its not a test class execution context, use real callout
@isTest
global class ResultHttpMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res;
System.debug('request inside mock '+req);
if(req != null) {
res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody(getBody());
res.setStatusCode(200);
res.setStatus('OK');
}
return res;
}
private static String getBody() {
String idmJsonMockResponse = '{"ednaScoreCard":{"sc":[],"etr":[{"test":"id:0","details":"false","ts":1471432074763,"stage":"1"},{"test":"ed:37","fired":false,"details":"ed:37(false) = true","ts":1471432074763,"stage":"1"}
}
在你的 Class A 和 Class B 中,保存对 ResultHttpMock
的引用
global class HttpCalloutClass {
public static HttpCalloutMock httpMock = null;
......
if(Test.istestRunning() && httpMock != null) {
Http response = httpMock.respond(request);
} else {
Http response = http.send(request);
}
}
在您的测试中 class,在调用 functionA() 和 functionB() 之前初始化 httpMock 对象
@isTest(seeAllData=false)
public class TestHttp {
static testMethod void testfunctionA() {
Test.startTest();
HttpCalloutClass.httpMock = new ResultHttpMock ();
String result = HttpCalloutClass.functionA();
Test.stopTest();
}
我有一个顶点 class A。我从 A 中调用 B class 的函数。
class A{
B bobj=new B();
B.function2();
function1(); //within class A
}
这个函数 1 和函数 2 是对 2 个 diff rest 服务的 http 调用。
我用 httpcallmockouts 为 function1 和 function2 写了一个测试 class。所以 APex Test class-
中的顺序是这样的B objB=new B();
HttpResponse res = CalloutB.getInfoFromExternalService();
A objA=new A();
HttpResponse res = CalloutA.getInfoFromExternalService();
//Till this point my test runs successfully for http calls mock
Test.startTest();
objA.function1();
//Here I get an error http callout not supported for test methods for function2 of B class.
//If I change the order above to
//A objA=new A();
//HttpResponse res = CalloutA.getInfoFromExternalService();
//B objB=new B();
//HttpResponse res = CalloutB.getInfoFromExternalService();
//Then I get error http callout not supported for test methods for function1 of A class.
Test.stopTest();
那么,如何在第一个 class.
中测试来自另一个 class 函数调用的 http 调用发生这种情况是因为您没有初始化 httpMock class 对象,因此您的测试 class 正在尝试访问测试 class 不允许的真实 Web 服务调用。你需要做些什么来初始化你的 mockClass 对象并使用它来代替 return 数据而不是访问真正的网络服务端点
In main class which does the callout declare a variable which holds reference to created mockClass object
Add a condition in your callout class which check if its a test class which is currently running using Test.isRunningTest() .
If its a test class then use your mock class to return data instead of real callout
If its not a test class execution context, use real callout
@isTest
global class ResultHttpMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res;
System.debug('request inside mock '+req);
if(req != null) {
res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody(getBody());
res.setStatusCode(200);
res.setStatus('OK');
}
return res;
}
private static String getBody() {
String idmJsonMockResponse = '{"ednaScoreCard":{"sc":[],"etr":[{"test":"id:0","details":"false","ts":1471432074763,"stage":"1"},{"test":"ed:37","fired":false,"details":"ed:37(false) = true","ts":1471432074763,"stage":"1"}
}
在你的 Class A 和 Class B 中,保存对 ResultHttpMock
的引用global class HttpCalloutClass {
public static HttpCalloutMock httpMock = null;
......
if(Test.istestRunning() && httpMock != null) {
Http response = httpMock.respond(request);
} else {
Http response = http.send(request);
}
}
在您的测试中 class,在调用 functionA() 和 functionB() 之前初始化 httpMock 对象
@isTest(seeAllData=false)
public class TestHttp {
static testMethod void testfunctionA() {
Test.startTest();
HttpCalloutClass.httpMock = new ResultHttpMock ();
String result = HttpCalloutClass.functionA();
Test.stopTest();
}