我正在尝试使用Apex Trigger而不是进程构建器来集成Slack。我想出了一种使用Trigger执行它的方法,并且完全符合我的要求。但是,我无法获得正确的测试覆盖率,并且该方法也会被传递,没有错误。
我对这种类型的集成很新。我相信我错过了一些我无法弄清楚的东西。
我还有其他方法,我从Apex Class调用,但想想如果我可以覆盖一种方法,我将能够与其他人合作。见下文:
Apex触发器:(50% - 测试覆盖率):
在商机上触发SalesforceToSlackOppTrigger(插入后,更新后) {
Id NewRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New').getRecordTypeId();
List<Id> opptyIds = new List<Id>();
if(SalesforceToSlack.isFirstTime) {
for(Opportunity opp:Trigger.new){
SalesforceToSlack.isFirstTime = false;
Account acct = [SELECT Id, CRM_Owner__c FROM Account WHERE Id =: Opp.AccountId];
if((opp.RecordTypeId == NewRecordTypeId && opp.isClosed == true && opp.isWon == True && acct.CRM_Owner__c == null)
&& (opp.OwnerId != '00536000003AUNV' && opp.OwnerId != '00536000001i9UG')){
opptyIds.add(opp.Id);
if(opptyIds.size() > 0){
SalesforceToSlack.OppWonPushToSlack(opptyIds);
}
}
}
}
}
Apex Class(1% - 测试覆盖率):
公共类SalesforceToSlack { private static final String OppWonSlackURL ='WEBHOOK_URL'//为隐私而编辑
public static void OppWonPushToSlack(List opportunityId){
Id oppId = opportunityId [0]; //如果是批量,只先发布以避免Slack通道过载
机会o = [SELECT Id,Name,CloseDate,Account.Name,Owner.Name
从机会
在哪里Id =:oppId];
Map msg = new Map();
msg.put('text','请将CRM所有者分配给我们在salesforce中的以下新机会:'+
'\ nOpportunity名称:'+ o.Name +''+
'\ n公司名称:'+ o.Account.Name +''+
'\ n关闭日期:'+ o.CloseDate +''+
'\ nOpportunity所有者:'+ o.Owner.Name +''+
'\ nLink:'+'https://cs14.salesforce.com/'+ o.id);
msg.put('mrkdwn',true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(OppWonSlackURL,'POST',body));
}
公共类QueueableSlackCall实现System.Queueable,Database.AllowsCallouts {
private final String url;
private final String method;
private final String body;
public QueueableSlackCall(String url, String method, String body) {
this.url = url;
this.method = method;
this.body = body;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
if (!Test.isRunningTest()) {
HttpResponse res = http.send(req);
}
}
}
}
Apex测试类:
@isTest 公共类SalesforceToSlackTest {
static testMethod void testOppWonPushToSlack() {
Id NewRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New').getRecordTypeId();
Id pricebookId = Test.getStandardPricebookId();
Account acct1 = new Account(Name = 'Test Account 1',
Company_Type__c = 'Trading',
Trading__c = 'Hedge Fund',
CRM_Owner__c = null);
Insert acct1;
Product2 prod = new Product2(Name = 'US',
isActive = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 10000,
IsActive = true
);
insert pbEntry;
Opportunity newOpp = new Opportunity(Name = 'New Opportunity',
CloseDate = System.today(),
StageName = '1. Initiated Evaluation',
Type ='New',
RecordTypeId = NewRecordTypeId,
AccountId = acct1.Id,
Payment_Terms__c = 'Annual',
Contract_Start_Date__c = System.today(),
Contract_End_Date__c = System.today() + 364,
Amount = 10000,
OwnerId = userinfo.getuserid());
Insert newOpp;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = newOpp.Id,
PricebookEntryId = pbEntry.Id,
Quantity = 1,
UnitPrice = pbEntry.UnitPrice
);
Insert oli;
Test.startTest();
newOpp.StageName = 'Contract Signed';
Update newOpp;
Test.stopTest();
Opportunity OppRecord = [select StageName from Opportunity where Id =: newOpp.Id];
System.assertEquals('Contract Signed',OppRecord.StageName);
}
}
答案 0 :(得分:0)
测试中不支持标注。您有两种选择: 1.使用test.isRunningTest()来避免在测试期间调用新的Http()。send()方法 2.使用HttpCalloutMock接口为代码中的callout提供模拟响应。 (我推荐这个,因为它更酷,更好的测试策略)