2012-06-12 15 views
0

我已經爲案例升級寫了一批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[] {'[email protected]'}; 

      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) 

{ 

} 


} 

回答

0

你不會輕易地用代碼來測試你的批處理。這是因爲當你創建你的測試數據時,它將會創建一個今天的創建日期,因此你的查詢定位器將不會返回任何記錄,並且execute方法將不會運行。

我調整了您的批次以使其可行。

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[] {'[email protected]'}; 
      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()`方法需要傳遞給它的第二個參數來限制範圍中的記錄數 - 您可以通過更改execute方法的結構來刪除它。唯一需要注意的是,最好創建多個記錄來測試批處理,但記住不要創建多個「範圍」值,因爲只能從測試中調用一個執行方法。