Apex batch class start method not running

apex-codesalesforce

i have a batch apex class

 global class apexBatch implements Database.Batchable<sObject>{
 global final string query;
List<user>  lstUser= new List<user>();
Set<id>     setUserID= new Set<id>();



//constructor
global apexBatch () {
    if (system.Test.isRunningTest())
    {
        this.query='SELECT id FROM user limit 100';
    }
    else
    {
        this.query='SELECT id FROM user ;
    }
}


global Database.QueryLocator start(Database.BatchableContext BC) {

    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {

// do some processing


}

global void finish(Database.BatchableContext BC) {

}

I am calling this class from test class using this code

       Test.startTest(); 
    apexBatch ba = new apexBatch();

    Database.executeBatch(ba);
      Test.stopTest();

When i check the code coverage i can only see that the constructor is covered, the start and execute methods are not covered at all.

Any idea what could cause this

Thanks

Best Answer

Are there any exceptions in your debug log when you run tests? This is the exact same method I use for testing batch classes, so I took this code (I know it's simplified), added the missing close quote on the second query (I assume your code did save correctly and this isn't the problem!), and put the test code into a class, sure enough it's covered the batch code correctly.

Finally, I have seen some weird issues with test coverage reporting recently — how are you running the tests at the moment? I just ran all tests in the Org and got 90% coverage (it missed the second query line since for obvious reasons).

Related Topic