提高测试类覆盖率

时间:2016-01-07 09:29:01

标签: xcode testing salesforce apex

任何人都可以帮助我增加代码覆盖率。对于下面的类和测试类。

类别:

global class Clear_Territory_Flag_Batch implements Database.Batchable<sObject> {        

    global String sql;     

    global Database.QueryLocator start(Database.BatchableContext BC) {


   sql='select id,Territory_Update_Falg__c from opportunity where 

Territory_Update_Falg__c=true and lastmodifieddate=today';

     return Database.getQueryLocator(sql);       

    }

     global void execute(Database.BatchableContext BC, List<SObject> objs) {

       List<Opportunity> Opptys=new List<Opportunity>();

       for(Opportunity o:(List<Opportunity>)objs){

         o.Territory_Update_Falg__c=false;
         Opptys.add(o);

       }

       if(Opptys.size()>0){

          OpportunityTriggerHandler.skipTriggerProcessing = true; 
             update Opptys;
          OpportunityTriggerHandler.skipTriggerProcessing = false;
          }   
     }

     global void finish(Database.BatchableContext BC) {

       }
}

测试类:

@isTest(SEEALLDATA=TRUE)    

  public class TestClear_Territory_Flag_Batch {    



    static testMethod void testMethod1()    


    {     


   List<Opportunity> st=new List<Opportunity>([Select id , Name , Territory_Update_Falg__c from Opportunity where Territory_Update_Falg__c = true  ]);    


    for(integer i=0;i<1500;i++) {    

   Opportunity t=new Opportunity();    

    t.Territory_Update_Falg__c = true;    


    //t.id='0000000123';
    st.add(t);
 }

    Database.BatchableContext bc;    

    Clear_Territory_Flag_Batch obj =new Clear_Territory_Flag_Batch();    

   //st=obj.start(b);    

  obj.execute(bc,st);    

   obj.finish(bc);    

   }    



}

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

@isTest

公共类TestClear_Territory_Flag_Batch {

static testMethod void testMethod1()    


{     
 List<Opportunity> oppListToInsert=new List<Opportunity>();
//insert some Opportunity records
for(integer i=0;i<10;i++) {    

    Opportunity opp=new Opportunity();   
    opp.Name ='Test Opp'+i;
    opp.Territory_Update_Falg__c = true;    
    //assign every require fields like closedate, stagename etc

    oppListToInsert.add(opp);
 }
try{
    insert oppListToInsert;
}catch(Exception e){
    system.debug('******Error in--'+e.getMessage());
}

Clear_Territory_Flag_Batch obj =new Clear_Territory_Flag_Batch();    
database.executeBatch(obj,5);

}

}

请告诉我它是否适合您。谢谢!

答案 2 :(得分:0)

 Create a new test method for this condition to make this condition false 
  

如果(Opptys.size()&0){

      OpportunityTriggerHandler.skipTriggerProcessing = true; 
         update Opptys;
      OpportunityTriggerHandler.skipTriggerProcessing = false;
      }