Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Salesforce 如何为opportunity上的apex触发器编写测试类_Salesforce_Apex Code_Apex_Sfdc - Fatal编程技术网

Salesforce 如何为opportunity上的apex触发器编写测试类

Salesforce 如何为opportunity上的apex触发器编写测试类,salesforce,apex-code,apex,sfdc,Salesforce,Apex Code,Apex,Sfdc,我已经创建了一个触发器,它调用一个future类来对第三方url进行http调用,在这里一切正常,但是测试类没有覆盖opportunity字段IsWon和IsClosed。我需要在测试类中做什么修改,使这个触发器的代码覆盖率至少达到75% //顶点触发器 trigger oppTrigger on Opportunity (before update) { String oppType = ''; for(Opportunity opp : Trigger.new){ if (opp.IsC

我已经创建了一个触发器,它调用一个future类来对第三方url进行http调用,在这里一切正常,但是测试类没有覆盖opportunity字段IsWon和IsClosed。我需要在测试类中做什么修改,使这个触发器的代码覆盖率至少达到75%

//顶点触发器

trigger oppTrigger on Opportunity (before update) {

String oppType = '';
for(Opportunity opp : Trigger.new){

if (opp.IsClosed == true){  // closed
  if (opp.IsWon == true){
    oppType = 'Won'; // closed-won
   }else{
    oppType = 'Lost'; // closed-lost
   }
} else { // open
       oppType = 'Open'; 
    }
  // call a method with @future annotation
   futureCls.srvcCallout(opp.id,opp.Amount,oppType); 
 }
}
//具有future方法的触发器的future类

global class futureCls {
@future(callout=true)
Public static void srvcCallout(String oppId, Decimal oppAmt, String oppType){

     // Create http request
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setHeader('Content-Type', 'application/json;charset=UTF-8');     
     req.setEndpoint('https://www.testurl.com/salesforce/opp-change'+'?id='+oppId+'&amt='+oppAmt+'&stage='+oppType);

     // create web service
     Http http = new Http();
      try {
        // Execute web service call here     
        HTTPResponse res = http.send(req);  
        // Debug messages
        System.debug('RESPONSE:'+res.toString());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('BODY:'+res.getBody());

        } catch(System.CalloutException e) {
             // Exception handler
             System.debug('Error connecting to Paperless..');
       }   
     }
 }
//我被卡住的触发器的测试类:-

@isTest
private class futureCls_Test {  

 private static testMethod void srvcCallout_Test() {        

    Test.startTest();

    // Unit test to cover trigger update event
    Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
    insert opp;
    opp.Amount = 1000;
    opp.StageName = 'Closed/Won';
    update opp;

    // Assign some test values
    String oppId = '1sf2sfs2';
    Decimal oppAmt = 4433.43;
    String oppType = 'Won';

    // Unit test to cover future method
    futureCls.srvcCallout(oppId, oppAmt,oppType);    

    // Unit test to cover http web service
    Test.setMock(HttpCalloutMock.class, new futureClsCalloutMock()); 
    Test.stopTest();

  }
}

您的测试类必须执行以下操作才能触发所有触发:

注意,这只是一种方法,你可以用几种不同的方法

  • 创造新的机会
  • 将opportunity更新为“打开”状态
  • 创造新的机会
  • 将opportunity更新为Closed/Lost
  • 创造新的机会
  • 将opportunity更新为Closed/Won
如果您问我,创建opportunity并将其更新为指定状态的TestDataFactory函数将非常有用:

@isTest
public testOpportunityWithStatusChange(targetStatus){
    //do stuff here
};

然后,您可以为要在测试类中检查的每个状态调用该工厂一次,以覆盖触发器。

在您为包含测试类所做的编辑中,您只需为触发器中的if/else语句中调用的每个状态重复机会创建和更新。