function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tushar SethTushar Seth 

Batch Apex Test Coverage Problem

Hello,

 

in scheduler we have the String query which will be passed to Batch Class but when I am calling the scheduler Test class it is showing 100% coverage for scheduler but when I see the Batch class coverage it is coming only 56% and also it is not covering  "return Database.getQueryLocator(query);" method.

 

Geeks help me out,

 

Many Thanks in advance,

Tushar

Jeff MayJeff May

here is a template of a batchable, schedulable test method.  Note that my class has a string for the SOQL called str_SOQL which I specify in my test method.

 

    @isTest
    public static void testmyBatch() {

        Test.StartTest();
        myBatchClass bc = new myBatchClass();
        bc.str_SOQL = 'Select id, Name ' +
                             ' from User where IsActive = true LIMIT 1';
        ID batchprocessid = Database.executeBatch(bc);
        
        Test.StopTest();
    }     

 

Tushar SethTushar Seth

Thanks JeffM, 

I had written the Test classes for scheduler earlier by using system.schedule(........) and yes as per scenarios it depends.

 

But if you use the string query in scheduler class and pass it to the batch class thru constructor and then write the test class what I think, it should have automatically covers your both the classes.

Earlier also I used the way which you have mentioned in your post, then it was not covering scheduler class.

 

Now what I did is that I used both the ways which is covering scheduler as well as Batch.

 

If you share the reason why we should use Database.execute(...) method when it would already covered by scheduler?

 

Well Thanks again JeffM