1

Source : Please check this link about test class coverage for batch apex

My Requirement : What they doing is they having argument contructor on batch apex. like

 global class CleanUpRecords implements Database.Batchable<sObject>,Database.Stateful{

         global CleanUpRecords(string query){    //argument constructor

            query = '.............';
         }
   ......
  }

for the above argument constructor they did test code coverage like below,

           string query = '..........';
           Test.startTest();
           CleanUpRecords c = new CleanUpRecords(query);
           Database.executeBatch(c);
           Test.stopTest();

But for my scenario I have default constructor(no-argument constructor ). So, How do I use above method to cover my default constructor. for example my construct is like,

global class CleanUpRecords implements Database.Batchable<sObject>,Database.Stateful{

    global string query;
             global CleanUpRecords(){    //no-argument constructor

                  query = '.............';
             }
       ......
      }

Any help is appreciable. Thanks!!

Azar

1 Answer 1

1

It is virtually the same...you just call the constructor you have.

Test.startTest();
CleanUpRecords c = new CleanUpRecords(); //<-- invoke constructor without argument
Database.executeBatch(c);
Test.stopTest();

By not providing the argument, the no-args constructor will be invoked.

If you had both, then you'd probably want to create two test methods (one for each constructor) to ensure that each gets coverage.

5
  • I tried the same one but no improvement in code coverage! Jan 29, 2016 at 11:24
  • That's curious. Normally, this is all you would need to do to invoke the constructor. Are you certain this is the part that is not getting code coverage?
    – pchittum
    Feb 1, 2016 at 0:04
  • ok I got my coverage to 87%. Some of the conditions are not coverable in test class so I used seeAllData=true annotation and set up records that satisfy those conditions. Feb 1, 2016 at 6:59
  • So is it a question of the query not returning any data? If so, there may be some work arounds. I think you should add some more details...maybe someone can help and you can avoid SeeAllData=true.
    – pchittum
    Feb 1, 2016 at 12:25
  • @mohammedazarudeen what did you do to increase your coverage? I'm seeing the same issue. Dec 14, 2016 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .