模拟 Webservice 调用实现
Mock Webservice Call out implementation
Salesforce 中的最近工作测试 classes 由于此错误而开始失败
定义为 TestMethod 的方法不支持 Web 服务标注
堆栈跟踪:null
但不明白在测试中在哪里实现调用class。
我创建了一个 Mock Webserivce class 但不知道在哪里调用 mock class。
这是失败的测试 class:
@isTest
public class OppLineItemInventoryTriggerTest {
static testmethod void testdemo() {
Account act = new Account(lastName = 'Testing', Billingstreet = '1234 Main', billing_as_shipping__c = True, Business_Unit__c = 'Vaya');
insert act;
Product2 prod = TestUtils.createProduct('product1');
Id RecordTypeIdopp = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Package Lot').getRecordTypeId();
Opportunity opp = new Opportunity();
opp.AccountId = act.ID;
opp.Name = 'Test';
opp.Status__c = 'Completed';
opp.Transfer_Type__c = 'Delivery';
opp.StageName = 'New';
opp.recordtypeid = RecordTypeIdopp;
opp.CloseDate = Date.newInstance(2016, 12, 9);
insert opp;
Dosage__c dos = new Dosage__c();
dos.Product__c = prod.ID;
dos.Unit__c = 'mg';
dos.Value__c = 5;
insert dos;
Product_Lot__c prolo = new Product_Lot__c();
insert prolo;
String standardPricebookId = Test.getStandardPricebookId();
PricebookEntry pdb = new PricebookEntry(Pricebook2Id = standardPricebookId, Product2Id = prod.Id, IsActive = true, UnitPrice = 100);
insert pdb;
Opportunity opps = [Select id from Opportunity Limit 1];
Contact con = new Contact(LastName = 'TestName');
insert con;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opps.Id, PricebookEntryId = pdb.Id, Quantity = 10, Shipping_Datetime__c = NULL, TotalPrice = 3000);
insert oli;
Inventory__c inv = new Inventory__c();
inv.Product_ID__c = prod.ID;
inv.Status__c = 'Completed';
inv.Opportunity__c = opp.ID;
inv.Days__c = '30';
inv.Prescription__c = 'www.google.com';
inv.Prescription_Line_Item_Id__c = 'test';
inv.Product_Lot__c = prolo.ID;
inv.Purchase_Date__c = Date.newInstance(2016, 12, 9);
insert inv;
sObject[] sObjectOldList = new sObject[] {};
sObject[] sObjectNewList = new sObject[] {};
sObject s11,s12;
//sObjectOldList.add(oli);
//sObjectNewList.add(oli);
OppLineItemInventoryTrigger s1 = new OppLineItemInventoryTrigger(sObjectOldList, sObjectNewList);
// s1.execute(sObjectOldList, true);
s1.execute(sObjectNewList, true);
s1.executable(s11, s12);
}
我创建了这个模拟网络服务class
global class YourHttpCalloutMockImpl implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HTTPResponse();
res.setHeader('Content-Type', 'application/JSON');
res.setBody('Your body');
res.setStatusCode(201);
return res;
}
}
但不确定如何在测试中调用它class。
您不需要在单元测试中显式调用模拟 class。相反,您可以使用 Test.setMock()
调用来配置它,例如
Test.setMock(WebServiceMock.class, new YourHttpCalloutMockImpl ());
对于 SOAP 网络服务,或
Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl ());
确保在调用要测试的代码之前执行此调用,这会发出标注。系统将自动使用您的模拟 class 响应出站 Web 服务调用 - 无需您进一步干预。
现在,您似乎正在查看 SOAP 文档,但已经编写了一个 HTTPCalloutMock
- 确保您采用的模拟接口与您的代码进行的调用类型相匹配。
定义为 TestMethod 的方法不支持 Web 服务标注堆栈跟踪:null
出现此错误是因为测试 class 中不允许 Web 服务标注。
要绕过标注,请添加 HttpcalloutMock class。
这是一个示例测试 class,使用模拟 class 来绕过 Web 服务标注。
************************ 测试 Class ****************** **********
@isTest
private class Test_class {
private class RestMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
String fullJson = 'your Json Response';
HTTPResponse res = new HTTPResponse();
res.setHeader('Content-Type', 'text/json');
res.setBody(fullJson);
res.setStatusCode(200);
return res;
}
}
static testMethod void service_call() {
Test.setMock(HttpCalloutMock.class, new RestMock());
Test.startTest();
//your web service call code
Test.StopTest();
}
}
模拟 class 将处理 Web 服务标注。
Salesforce 中的最近工作测试 classes 由于此错误而开始失败
定义为 TestMethod 的方法不支持 Web 服务标注 堆栈跟踪:null
但不明白在测试中在哪里实现调用class。
我创建了一个 Mock Webserivce class 但不知道在哪里调用 mock class。
这是失败的测试 class:
@isTest
public class OppLineItemInventoryTriggerTest {
static testmethod void testdemo() {
Account act = new Account(lastName = 'Testing', Billingstreet = '1234 Main', billing_as_shipping__c = True, Business_Unit__c = 'Vaya');
insert act;
Product2 prod = TestUtils.createProduct('product1');
Id RecordTypeIdopp = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Package Lot').getRecordTypeId();
Opportunity opp = new Opportunity();
opp.AccountId = act.ID;
opp.Name = 'Test';
opp.Status__c = 'Completed';
opp.Transfer_Type__c = 'Delivery';
opp.StageName = 'New';
opp.recordtypeid = RecordTypeIdopp;
opp.CloseDate = Date.newInstance(2016, 12, 9);
insert opp;
Dosage__c dos = new Dosage__c();
dos.Product__c = prod.ID;
dos.Unit__c = 'mg';
dos.Value__c = 5;
insert dos;
Product_Lot__c prolo = new Product_Lot__c();
insert prolo;
String standardPricebookId = Test.getStandardPricebookId();
PricebookEntry pdb = new PricebookEntry(Pricebook2Id = standardPricebookId, Product2Id = prod.Id, IsActive = true, UnitPrice = 100);
insert pdb;
Opportunity opps = [Select id from Opportunity Limit 1];
Contact con = new Contact(LastName = 'TestName');
insert con;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opps.Id, PricebookEntryId = pdb.Id, Quantity = 10, Shipping_Datetime__c = NULL, TotalPrice = 3000);
insert oli;
Inventory__c inv = new Inventory__c();
inv.Product_ID__c = prod.ID;
inv.Status__c = 'Completed';
inv.Opportunity__c = opp.ID;
inv.Days__c = '30';
inv.Prescription__c = 'www.google.com';
inv.Prescription_Line_Item_Id__c = 'test';
inv.Product_Lot__c = prolo.ID;
inv.Purchase_Date__c = Date.newInstance(2016, 12, 9);
insert inv;
sObject[] sObjectOldList = new sObject[] {};
sObject[] sObjectNewList = new sObject[] {};
sObject s11,s12;
//sObjectOldList.add(oli);
//sObjectNewList.add(oli);
OppLineItemInventoryTrigger s1 = new OppLineItemInventoryTrigger(sObjectOldList, sObjectNewList);
// s1.execute(sObjectOldList, true);
s1.execute(sObjectNewList, true);
s1.executable(s11, s12);
}
我创建了这个模拟网络服务class
global class YourHttpCalloutMockImpl implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HTTPResponse();
res.setHeader('Content-Type', 'application/JSON');
res.setBody('Your body');
res.setStatusCode(201);
return res;
}
}
但不确定如何在测试中调用它class。
您不需要在单元测试中显式调用模拟 class。相反,您可以使用 Test.setMock()
调用来配置它,例如
Test.setMock(WebServiceMock.class, new YourHttpCalloutMockImpl ());
对于 SOAP 网络服务,或
Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl ());
确保在调用要测试的代码之前执行此调用,这会发出标注。系统将自动使用您的模拟 class 响应出站 Web 服务调用 - 无需您进一步干预。
现在,您似乎正在查看 SOAP 文档,但已经编写了一个 HTTPCalloutMock
- 确保您采用的模拟接口与您的代码进行的调用类型相匹配。
定义为 TestMethod 的方法不支持 Web 服务标注堆栈跟踪:null
出现此错误是因为测试 class 中不允许 Web 服务标注。
要绕过标注,请添加 HttpcalloutMock class。
这是一个示例测试 class,使用模拟 class 来绕过 Web 服务标注。
************************ 测试 Class ****************** **********
@isTest
private class Test_class {
private class RestMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
String fullJson = 'your Json Response';
HTTPResponse res = new HTTPResponse();
res.setHeader('Content-Type', 'text/json');
res.setBody(fullJson);
res.setStatusCode(200);
return res;
}
}
static testMethod void service_call() {
Test.setMock(HttpCalloutMock.class, new RestMock());
Test.startTest();
//your web service call code
Test.StopTest();
}
}
模拟 class 将处理 Web 服务标注。