顶级类的测试用例

时间:2012-06-12 05:53:13

标签: apex-code

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

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)

{

}


}

1 个答案:

答案 0 :(得分: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封电子邮件,因此在执行批处理时,您必须确保将scope参数传递给execute方法。话虽如此,我很想在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()`方法需要传递给它的第二个参数来限制作用域中的记录数 - 你可以通过改变execute方法的结构来删除它。唯一需要注意的是,最好创建多个记录来测试批处理,但记住不要创建多个“范围”值,因为只能从测试中调用一个执行方法。

相关问题