function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

Need a Test class for Scheduled Batch apex


 
Hi Gurus,

Please help me in writng a test class for scheduled batch apex below ,i want 100% code coverage please
 
global class ERTExtract255BatchClass implements Database.Batchable<sObject>, Database.Stateful {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID,Description,Case_Desc_255__c FROM Case' 
        );
    }
    global void execute(Database.BatchableContext bc, List<Case> scope){
        // process each batch of record
        List<Case> lstCase = new List<Case>();       
        for (Case cas : scope) {
            cas.Case_Desc_255__c = cas.Description.Left(255);
            lstCase.add(cas);  
        }   
        update lstCase;
    }   
    global void finish(Database.BatchableContext bc){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
    
      AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors,
      a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById,
      a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()];
       
      // below code will send an email to User about the status
      String[] email = new String[]{'.com'};
      mail.setToAddresses(email);
      mail.setReplyTo('abc@gmail.com'); 
      mail.setSenderDisplayName('ERT Extract First 255 Char Batch Processing');
      mail.setSubject('Batch Processing '+a.Status);
      mail.setPlainTextBody('The Batch Apex job processed '+ a.TotalJobItems+'batches with '+a.NumberOfErrors+'failures '+'Job Item processed are '+a.JobItemsProcessed);
      Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});
    }   
     
}

Thanks,
Fiona​​​​​​​



 
David Zhu 🔥David Zhu 🔥
You may refer to the code below:
    @IsTest
    static void testBatchJob(){
      
       List<Case> cases = new List<Case>();
       for (integer i =0;i<300;i++)
       {
            Case c = new Case();
            c.Description = 'aaaaaa'.rightPad(255,'b');
            c.status = 'new';
            c.Subject = 'test';
            //add other mandatory fields 
            cases.add(c);
        }

        insert cases;

        Test.startTest();
        Database.executeBatch(new ERTExtract255BatchClass());
        Test.stopTest();

        Case case = [Select id,Case_Desc_255__c from Case Limit 1];
        System.assertEquals(case.Case_Desc_255__c.length(),255);

    }


 
fiona gentryfiona gentry
Hi David thanks ,but i am getting below exception when I ran the above test
System.UnexpectedException: No more than one executeBatch can be called from within a test method.  Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
fiona gentryfiona gentry
here is the updated batch apex class
global class ERTExtract255BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID,Description,Case_Desc_255__c FROM Case' 
        );
    }
    global void execute(Database.BatchableContext bc, List<Case> scope){
        // process each batch of record
        List<Case> lstCase = new List<Case>();       
        for (Case cas : scope) {
            string Strdesc = cas.Description ; 
           if(Strdesc.length()>255){
            cas.Case_Desc_255__c = cas.Description.Left(255);
            lstCase.add(cas);  
}
        }   
        update lstCase;
    }   
    global void finish(Database.BatchableContext bc){
  
    }   
     
}