+ Start a Discussion
srinivas pulipatisrinivas pulipati 

Hi I am writing BatchApex class code is execute successfully but i wrote test class for batch apex it is also execute successfully but code coverage is show 57% plz check once

Batch Apex Class:

global class BatchClass implements database.Batchable<Sobject>{
    global database.QueryLocator start(Database.BatchableContext bc){
        String Query='select id from account';
        return Database.getQueryLocator(Query);
    }
    global void execute(Database.BatchableContext bc , List<Sobject> scope){
        List<Account> acc=new List<Account>();      ///These 3 line are not execute  when i run the test code
        for(account a : acc){

            system.debug('a');
            //acc.add(a);
        }
       // update acc;e
    }
    global void finish(Database.BatchableContext bc){
        System.debug('finished');
    }
}

Batch Test Class:

@istest
private class TestBatchClass{
    static testmethod void testbatchclass(){
        BatchClass bc=new BatchClass();
        string Query='select id from account limit 200';
        test.startTest();
        Database.executeBatch(bc,200);
         List<Account> acc=new List<Account>();
         //BatchableContext bc;
        //bc.execute(bc,acc);
        test.stopTest();
    } 
}


When i run the test class code coverage is Show 57%
 
ManojjenaManojjena
Hi Srinivas,
Try with below code .
 
@isTest
private class TestBatchClass {
    static testmethod void unitTest() {
      List<Account> accList=eew List<Account>();
       for (Integer i=0;i<10;i++) {
          Account acc=new Account();
		  acc.Name='Test'+i;
		  accList.add(acc);
       }
       insert accList;

       Test.startTest();
      BatchClass batCls=new BatchClass();
       Database.executeBatch(batCls);
       Test.stopTest();

    }
}
Let m eknow if it helps !!

Thanks
Manoj
 
ManojjenaManojjena
Hi Srinivas,

Your probelem is resolved or still you have issue .If resolved please cllose the thred by selecting answer to help others in forum those who are facing sam etype of issue .

Thanks
Mnaoj