+ 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 )
        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)
        list<Integer> rdlist=new list<Integer>();
           for(String row : scope){
             List<String> fields = row.split(',');
             rdlist.add(Integer.valueof(fields[3]));//--Order Product
   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){

         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){

                   // No Record found
        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

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 

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

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() + ']');
       InvoiceBatchClass c = new InvoiceBatchClass(b);
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