0

apex为案例升级编写了一个批处理类。我想为它写一个测试类。有人可以帮帮我吗。下面是我写的代码:

global class CaseEscalation  implements  Database.Batchable<SObject> 

{

global CaseEscalation()

{

}  

 global Database.QueryLocator start(Database.BatchableContext  ctx)

 {

  System.debug('checking=======');

     return Database.getQueryLocator([SELECT  CaseNumber, CreatedDate,Status FROM Case             where (Status!='Closed' AND Status!='Escalated') and CreatedDate<TODAY ]); 

 }


 global void execute(Database.BatchableContext ctx, List<Sobject> scope)

 {

   List<Case> ca = (List<Case>)scope;

   System.debug('checking======='+ca);

   for(Case c : ca)

   {      

             System.Debug(c);
             String emailMessage = 'The case  ' + c.CaseNumber + ' has been ecalated ' + ' Please look into the case ' + 'Thankyou';

             Messaging.SingleEmailMessage mail  =   new Messaging.SingleEmailMessage();

             String[] toAddresses = new String[] {'reems.agarwal3@gmail.com'};

             mail.setToAddresses(toAddresses);

             mail.setSubject('Case Escalation');

             mail.setPlainTextBody(emailMessage);

             Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });

             c.Status = 'Escalated';

    }

    if(ca.size()>0)

    update ca;

       }

global void finish(Database.BatchableContext ctx)

{

}


}
4

1 回答 1

0

您将无法以当前的方式轻松地使用代码测试您的批次。这是因为当您创建测试数据时,它将使用今天的创建日期创建,因此您的查询定位器不会返回任何记录,并且执行方法也不会运行。

我已经调整了你的批次以使其可行。

global class CaseEscalation implements Database.Batchable<SObject> {

    private static Date cutOff;

    global CaseEscalation(Date CutOffDate){
        cutOff = CutOffDate;
    }  

    global Database.QueryLocator start(Database.BatchableContext  ctx){
        return Database.getQueryLocator('SELECT Id, CaseNumber, CreatedDate, Status FROM Case WHERE (Status != \'Closed\' AND Status != \'Escalated\') and CreatedDate < :cutOff '); 
    }

    global void execute(Database.BatchableContext ctx, List<Sobject> scope){

        if(scope.size() == 0)
            return;

        for(Case c : (List<Case>)scope){      
            String emailMessage = 'The case  ' + c.CaseNumber + ' has been ecalated ' + ' Please look into the case ' + 'Thankyou';
            Messaging.SingleEmailMessage mail  =   new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'reems.agarwal3@gmail.com'};
            mail.setToAddresses(toAddresses);
            mail.setSubject('Case Escalation');
            mail.setPlainTextBody(emailMessage);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });

            c.Status = 'Escalated';
        }

        update scope;
    }

    global void finish(Database.BatchableContext ctx){

    }
}

一旦你有了这个,你就可以编写一些测试来证明它可以运行。您需要注意,您一次不能从批处理执行方法中发送超过 10 封电子邮件,因此在执行批处理时,您必须确保将范围参数传递给执行方法。话虽如此,我很想在 execute 方法的循环中简单地构建电子邮件的正文,然后发送一封电子邮件,其中包含更新记录的所有详细信息 - 一封电子邮件,一个更干净的收件箱,并且没有担心达到州长限制。我没有这样做,因为我想让它尽可能接近您的原始代码。

然后,您可以编写一个如下所示的测试脚本来练习代码。

@isTest
private class CaseEscalationTest {

    private static testMethod void testInCutOff(){

        List<Case> testCases = new List<Case>();

        for(Integer i = 0; i < 10; i++)
            testCases.add(new Case(Status='Open'));

        insert testCases;

        Test.startTest();

        CaseEscalation ce = new CaseEscalation(system.today().addDays(1));
        database.executeBatch(ce, 10);

        Test.stopTest();

        testCases = [SELECT Id FROM Case WHERE Status = 'Open'];

        system.assertEquals(0, testCases.size());

    }

    private static testMethod void testOUtOfCutOff(){

        List<Case> testCases = new List<Case>();

        for(Integer i = 0; i < 10; i++)
            testCases.add(new Case(Status='Open'));

        insert testCases;

        Test.startTest();

        CaseEscalation ce = new CaseEscalation(system.today());
        database.executeBatch(ce, 10);

        Test.stopTest();

        testCases = [SELECT Id FROM Case WHERE Status = 'Open'];

        system.assertEquals(10, testCases.size());

    }

}

测试中要注意的是使用,Test.startTest()Test.stopTest()会强制批处理在继续测试之前执行,从而使您有机会从数据库中选择更新的数据并确保它已按照您的预期进行更改。正如我之前所说,“database.executeBatch()”方法需要将第二个参数传递给它以限制范围内的记录数 - 您可以通过更改执行方法的结构来删除它。唯一要注意的另一件事是,为测试批次创建多个记录是一种很好的做法,但请记住不要创建多个“范围”值,因为您只能从测试中调用一个执行方法。

于 2012-06-12T19:01:38.970 回答