+ Start a Discussion
SFDC_LearnerSFDC_Learner 

Batch Apex Code Coverage

 1	  global class batchTestClass implements database.Batchable<sObject>{
 2	   public String query='';
 3	   public batchTestClass(){
 4	   query ='select id,name,city__c from DataLoad__c';
 5	   }
 6	  
 7	   global Database.QueryLocator start(Database.BatchableContext BC){
 8	   return Database.getQueryLocator(query);
 9	   }
 10	  
 11	   global void execute(Database.BatchableContext BC, List<DataLoad__c> lstD){
 12	   for(Integer i=0;i<lstD.size();i++){
 13	   if(lstD[i].city__c =='hyd'){
 14	   lstD[i].city__c='Banglore';
 15	   }
 16	   }
 17	   update lstD;
 18	   }
 19	  
 20	   global void finish(Database.BatchableContext BC){
 21	   }
 22	  
 23	   public static testmethod void testm1(){
 24	   test.startTest();
 25	   DataLoad__c objD = new DataLoad__c(name='DataLoadTest',city__c='hyd');
 26	   insert objD;
 27	  
 28	   batchTestClass objb = new batchTestClass();
 29	   database.executebatch(objb);
 30	   test.stopTest();
 31	   }
 32	  }

 Why 14th line is not covered.

asish1989asish1989

Hi

  Try this

   

         

public static testmethod void testm1(){
       test.startTest();
       DataLoad__c objD = new DataLoad__c(name='DataLoadTest',city__c='hyd');
       insert objD;
List<DataLoad__c> objDlist = new List<DataLoad__c>();

objDlist.add(objD);

batchTestClass objb = new batchTestClass();
database.executebatch(objDlist);
test.stopTest();
}

 

Did this post resolve your problem , if so please mark it solved so that others get benifited

 

 

 

Thanks

asish

SRKSRK

public batchTestClass()
    {
        query ='select id,name,city__c from DataLoad__c';
     }

In u r code at line number 3,4,5

create a record in system with city as 'hyd' line number 14 is coveted
i know  it is not a correct soluction test method does not have to be dependent on org data


also u can try @isTest(SeeAllData=true)

Prad@SFDCPrad@SFDC

Hi

   

public static testmethod void testm1(){
       test.startTest();
       DataLoad__c objD = new DataLoad__c(name='DataLoadTest',city__c='hyd');
       insert objD;
List<DataLoad__c> objDlist = new List<DataLoad__c>();

objDlist.add(objD);

batchTestClass objb = new batchTestClass();
database.executebatch(objDlist);
test.stopTest();
}

 

The solution is correct but in Executebatch method we need to pass the instance of batch class.


then Solution is


public static testmethod void testm1(){
       test.startTest();
       DataLoad__c objD = new DataLoad__c(name='DataLoadTest',city__c='hyd');
       insert objD;
       batchTestClass objb = new batchTestClass();
       database.executebatch(objb);
       test.stopTest();
}

 Try this code.............