为可调度的批处理类编写测试类

时间:2018-08-14 09:23:00

标签: salesforce apex

我编写了可调度的批处理类,用于向用户发送两种电子邮件,第一类是给机会阶段超过90天的用户,第二种是那些没有改变机会阶段的人,即少于或等于30天。 (仅以第二个条件为例)。它工作正常,但我的测试班仅覆盖23/53行。请帮助我覆盖100%的覆盖范围。 以下是**之间的行,这些行未在测试类别中涵盖。预先感谢!

我的批次分类:

 global class OppStageEmail implements Database.Batchable<sObject>, Database.Stateful, Schedulable {    
string fld = 'StageName';
date dt = system.now().date();
Date dd1 = dt - 90;

global database.QueryLocator start (database.BatchableContext BC){
    string s = 'Active';
    string query = 'select id,StageName,name,(select Id,CreatedDate from Histories order by CreatedDate desc limit 1) from Opportunity where Opportunity_Status__c = :s and id not in (select opportunityid from OpportunityFieldHistory where day_only(CreatedDate) >=: dd1  and Field = : fld)';
    return database.getQueryLocator(query);
}

List<Opportunity> oppFList = new List<Opportunity>();
List<Opportunity> oppLList = new List<Opportunity>();

global void execute(database.BatchableContext BC,List<Opportunity>scope){

    Set<Id> oppFEIds = new Set<Id>();
    Set<Id> oppLEIds = new Set<Id>();

    for(Opportunity o : scope){
        for(OpportunityFieldHistory h : o.Histories){
            **Date cd = date.newinstance(h.CreatedDate.year(),h.CreatedDate.month(),h.CreatedDate.day());
            integer totalDays = cd.daysBetween(dt); 

            if(totalDays > 90){
                oppFEIds.add(o.Id);
            }
            if(totalDays <= 30){
                oppLEIds.add(o.Id);
            }**
        }
    }   
    oppFList = [select Id,name ,Owner.Email,OwnerId from opportunity where Id IN :oppFEIds];
    oppLList = [select Id,name ,Owner.Email,OwnerId from opportunity where Id IN :oppLEIds];
    SendMail();
}

global void finish(database.BatchableContext BC){
}

**global void execute(SchedulableContext SC){
    OppStageEmail o = new OppStageEmail();
    Id batchProcessId = Database.executeBatch(o);
}**

public void SendMail(){
        List<Messaging.SingleEmailMessage> lstEmail1 = new List<Messaging.SingleEmailMessage>();    
        List<Messaging.SingleEmailMessage> lstEmail2 = new List<Messaging.SingleEmailMessage>();    

        if(oppFList.size() > 0){
            **EmailTemplate et1 =[Select id,HtmlValue from EmailTemplate where Name=:Constants.OppEmailTemplate1]; 

            for(integer i=0; i< oppFList.size(); i++){
                messaging.SingleEmailMessage semail1 = new messaging.SingleEmailMessage(); 
                semail1.setTargetObjectId(oppFList[i].OwnerId);
                semail1.saveAsActivity = false;
                semail1.setSubject(Constants.OppEmail1Subject);
                semail1.setHtmlBody('Helloo '+ oppFList[i].Name + ',' + '\n\n This is reminder Email');
                semail1.setTemplateId(et1.id);
                lstEmail1.add(semail1);
            }
            messaging.sendEmail(lstEmail1);
        }**
        if(oppLList.size() > 0){
            **EmailTemplate et2 =[Select id,HtmlValue from EmailTemplate where Name=:Constants.OppEmailTemplate2]; 

            for(integer i=0; i< oppLList.size(); i++){      
                messaging.SingleEmailMessage semail2 = new messaging.SingleEmailMessage(); 
                semail2.setTargetObjectId(oppLList[i].OwnerId);
                semail2.saveAsActivity = false;
                semail2.setSubject(Constants.OppEmail2Subject);
                semail2.setHtmlBody('Helloo '+ oppLList[i].Name + ',' + '\n\n This is reminder Email');
                semail2.setTemplateId(et2.id);
                lstEmail2.add(semail2);
            }
            messaging.sendEmail(lstEmail2);**
        }
}
  }

上述批处理类的测试类:

   @isTest
  public class OppStageEmailTest {
public static testmethod void Method1(){

    List<Opportunity> opp = new List<Opportunity>();
    //List<Opportunity> opp1 = new List<Opportunity>();
    Date dt = System.now().date() + 30;

    for(integer i=0; i<200; i++){
        Opportunity op = new Opportunity(Name='O' + i,Opportunity_Status__c='Active',StageName='Prospecting',CloseDate = dt);
        opp.add(op);
    }
    insert opp;

    test.startTest();
    OppStageEmail oe= new OppStageEmail();
    Database.executeBatch(oe);
    test.stopTest();
}
 }

0 个答案:

没有答案