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
sfdc demo 1sfdc demo 1 

test class for batch apex code

Here is my apex code: 
global class InvoiceBatchClass implements Database.Batchable<string>, Database.Stateful{
                
            global final blob dataDocuments;
            global boolean entry1;
            global boolean entry;
            global boolean b;
           
    global InvoiceBatchClass (Blob data )
     {
                this.dataDocuments=data;
     }
          
        global Iterable<string>  start(Database.BatchableContext BC){
        string nameFile=this.dataDocuments.toString();        
        return new CSVIterator(this.dataDocuments.toString(), '\n');
    }
    
     global void execute(Database.BatchableContext BC, List<String> scope)
     { 
        entry1=false;
         entry=false;
        
        list<Integer> rdlist=new list<Integer>();
           for(String row : scope){
             if(entry){
             List<String> fields = row.split(',');
             
             rdlist.add(Integer.valueof(fields[3]));//--Order Product
             
             }
             else{
                 entry=true;
             }
         }
   List<Order> Checkrdlist=[SELECT ID,name,Order_No_From__c  from Order WHERE Order_No_From__c IN:rdlist];
       list<Invoice__c> InvoiceList=new list<Invoice__c>();
          
       for(String row : scope){
    if(entry1){

        b=false;
       system.debug('abc'+row);
       
         List<String> fields = row.split(',');
              
             Invoice__c Invoices=new Invoice__c();
             
              
               String mDate=String.valueof(fields[0]);
               String[] stro = mDate.split(' ');
               String[] dtso = stro[0].split('/');
               Date myDate1 = date.newinstance(Integer.valueOf('20'+dtso[2]), Integer.valueOf(dtso[1]), Integer.valueOf(dtso[0])); 
             
              String invoiceDateFrom=String.valueof(fields[1]);
               String[] str1 = invoiceDateFrom.split(' ');
               String[] dts1 = str1[0].split('/');
               Date myDate2 = date.newinstance(Integer.valueOf('20'+dts1[2]), Integer.valueOf(dts1[1]), Integer.valueOf(dts1[0])); 
            
            
            String invoiceDateTo=String.valueof(fields[2]);
               String[] str2 = invoiceDateTo.split(' ');
               String[] dts2 = str2[0].split('/');
               Date myDate3 = date.newinstance(Integer.valueOf('20'+dts2[2]), Integer.valueOf(dts2[1]), Integer.valueOf(dts2[0])); 
             
             Integer ORD=Integer.valueof(fields[3]);  
//Assign Order Object Fields....
            for(Order a:Checkrdlist){
                System.debug('aaa-hhhhh-'+a.Order_No_From__c+ORD);
               
                if(a.Order_No_From__c==ORD){
                b=true;
                Invoices.Order__c=a.id;
                Break;
                }
                
                
            }
              system.debug('firstdate'+myDate1);
               system.debug('seconddate'+myDate2);
              

                 Invoices.Invoice_Date__c=myDate1;   
                 Invoices.Invoice_Date_From__c=myDate2;   
                 Invoices.Invoice_Date_To__c=myDate3;    
                 //Invoices.Order__c='801610000009DH8';
                 Invoices.OrderNumberFrom_Ex__c=Integer.Valueof(ORD);
                 if(b==true){
                 InvoiceList.add(Invoices);
                     }
                   else{
                   // No Record found
                   }                          
                 system.debug('abc'+InvoiceList);
                   }
             
             else{
                 entry1=true;
             }
       
       }
        upsert InvoiceList OrderNumberFrom_Ex__c;
  }
    

     global void finish(Database.BatchableContext BC)
     {
     
         // Send an email to the Apex job's submitter notifying of job completion.  
       
     }   
     
}


Tried so many test code but fail to do it, Please can any one help me for this code. Need test class for this apex class

Thank you in advance
AshlekhAshlekh
Hi,

Please post the code which you have written for coverage in test class. 

Here is link provide by salesforce how to write test class for batch 
https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm

-Thanks
Ashlekh Gera
sfdc demo 1sfdc demo 1
here it is a test class , i am inserting data using csv file, so parsing the string .Please have a look

@isTest
private class InvoiceBatchTest_UT {

    static testmethod void test() {
     
            String strOriginal = '11/06/13\n';
            System.debug('String strOriginal: [' + strOriginal + ']');
            
            String strUrlUTF8 = EncodingUtil.urlEncode(strOriginal, 'UTF-8');
            System.debug('String strUrlUTF8: [' + strUrlUTF8 + ']');
            
           Blob  b = Blob.valueOf(strUrlUTF8);
            System.debug('Blob b: [' + b.toString() + ']');
        
           
       Test.startTest();
       InvoiceBatchClass c = new InvoiceBatchClass(b);
       Database.executeBatch(c);
       Test.stopTest();
 
}
}
Cloud_forceCloud_force
As best practice and keep it simple; To cover batch apex in test class make sure you pull out all the logic from excute method and put it in some apex class, this will allow you too fully cover batch apex class seperatly and you can cover the apex class seperatly.

Following such a strategy will reduce your headech fo all solving all teh other errors that are reakted to no records processed by batch apex in a test class. By following ablve strategy you can cover the batch apex by simply calling it without any proper data.

see if this helps: here is a sample test class for batch apex http://www.cloudforce4u.com/2013/07/test-class-for-batch-apex.html