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
Phuc Nguyen 18Phuc Nguyen 18 

Database.BatchableContext no coverage

Hello All,
Trying to figure out why I have no coverage for BatchableContext.
Here is the Class
global void execute(Database.BatchableContext BC, List<GridView__c> scope) {
        system.debug('scope ' + scope);
        for (GridView__c grid : scope) {
            grid.Public__c = false;
        }
        update scope;
    }

Here is the Test Class
private class MakePrivateTest {
    @istest
    private static void MakePrivateTestTrackers() {
        list<GridView__c> tList = new List<GridView__c>{
            new GridView__c(
                Name = 'test1',
                Public__c = false,
                LastViewedDate__c = null
            ),
            new GridView__c(
                Name = 'test2',
                Public__c = true,
                LastViewedDate__c = System.now().addHours(56 * -1)
            ),
            new GridView__c(
                Name = 'test4',
                Public__c = true,
                LastViewedDate__c = System.now().addHours(95 * -1)
            )
        };
        insert tList;

        test.startTest();
        SchedulableContext sc = null;
        tmp.execute(sc);
        MakePrivate tmp = new MakePrivate();
        ID batchprocessid = Database.executeBatch(tmp);
        test.stopTest();

       tList = [SELECT Id, Name, Public__c, LastViewedDate__c FROM GridView__c];
        system.assertEquals(3, tList.size());
    }
}

But no coverage for "global void execute(Database.BatchableContext BC"
ANy suggestions would be apreciated.
Thank you
P
 
Best Answer chosen by Phuc Nguyen 18
tbrookstbrooks
Hey Phuc,

In your test class you are using the method addHours but in your private method you are using method addDays. Because your query is looking for values less than (before) 91 days in the past when you are using the addHours it won't match the criteria and have no records to pass as the scope to the execute. 

I would change as such: 
private class MakePrivateTest {
    @istest
    private static void MakePrivateTestTrackers() {
        list<GridView__c> tList = new List<GridView__c>{
            //Below won't match criteria as last modified date wont be 91 days in past
            new GridView__c(
                Name = 'test1',
                Public__c = false,
                LastViewedDate__c = null
            ),
          //Below won't match criteria as last viewed date wont be 91 days in past
            new GridView__c(
                Name = 'test2',
                Public__c = true,
                LastViewedDate__c = System.now().addDays(-56)
            ),
            //Below will match criteria as last viewed date will be more than 91 days in past
            new GridView__c(
                Name = 'test4',
                Public__c = true,
                LastViewedDate__c = System.now().addDays(-95)
            )
        };
        insert tList;

        test.startTest();
        MakePrivate tmp = new MakePrivate();
        ID batchprocessid = Database.executeBatch(tmp);
        test.stopTest();

      list<GridView__c> postBatchList = [SELECT Id, Name, Public__c, LastViewedDate__c FROM GridView__c Where Public__c = false];
        system.assertEquals(1, postBatchList .size());
//Assert that only the one record that would match query criteria is set to false.
    }
}


Please let me know if this helps and if this solves your issue mark it as best answer!
Regards,
Tyler

All Answers

Agustin BAgustin B
Hi Phuc, what is on the start method of the batch? Are you getting GridView__c values there that will pass on the execute method?

You can check this for reference: https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch
 
Agustin BAgustin B
If you ran your query in the query editor with the same parameters as in the test, you get results?
Also, why dont you put GridView__c instead of the strk__StGridView__c in the execute parameter. Is it another object or the prefix?
Phuc Nguyen 18Phuc Nguyen 18
Hey Agustin,
That was a typo.
global void execute(SchedulableContext SC) {
        Id batchInstanceId = Database.executeBatch(new MakePrivate());
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        Datetime fromDateTime = getPrivateBeforeDateTime();
        return Database.getQueryLocator(
            [
                SELECT Id, Public__c
                FROM GridView__c
                WHERE
                    (Public__c = true
                    AND LastViewedDate__c != null
                    AND LastViewedDate__c < :fromDateTime)
                    OR (Public__c = true
                    AND LastViewedDate__c = null
                    AND LastModifiedDate < :fromDateTime)
            ]
        );
    }

    global void execute(Database.BatchableContext BC, List<GridView__c> scope) {
       for (GridView__c trk : scope) {   ///no code coverage
            trk.Public__c = false;    ///no code coverage
        }
        update scope;    ///no code coverage
    }

    global void finish(Database.BatchableContext BC) {
    }

 
    @TestVisible
    private static Datetime getPrivateBeforeDateTime() {
        // subtract days to keep from the current date/time
        Datetime fromDateTime = System.now().addDays(91 * -1);
        system.debug('dateminus ' + fromDateTime);
        return fromDateTime;
    }
And do you want me to run this query?
SELECT Id, Name, Public__c, LastViewedDate__c FROM GridView__c]


 
tbrookstbrooks
Hey Phuc,

In your test class you are using the method addHours but in your private method you are using method addDays. Because your query is looking for values less than (before) 91 days in the past when you are using the addHours it won't match the criteria and have no records to pass as the scope to the execute. 

I would change as such: 
private class MakePrivateTest {
    @istest
    private static void MakePrivateTestTrackers() {
        list<GridView__c> tList = new List<GridView__c>{
            //Below won't match criteria as last modified date wont be 91 days in past
            new GridView__c(
                Name = 'test1',
                Public__c = false,
                LastViewedDate__c = null
            ),
          //Below won't match criteria as last viewed date wont be 91 days in past
            new GridView__c(
                Name = 'test2',
                Public__c = true,
                LastViewedDate__c = System.now().addDays(-56)
            ),
            //Below will match criteria as last viewed date will be more than 91 days in past
            new GridView__c(
                Name = 'test4',
                Public__c = true,
                LastViewedDate__c = System.now().addDays(-95)
            )
        };
        insert tList;

        test.startTest();
        MakePrivate tmp = new MakePrivate();
        ID batchprocessid = Database.executeBatch(tmp);
        test.stopTest();

      list<GridView__c> postBatchList = [SELECT Id, Name, Public__c, LastViewedDate__c FROM GridView__c Where Public__c = false];
        system.assertEquals(1, postBatchList .size());
//Assert that only the one record that would match query criteria is set to false.
    }
}


Please let me know if this helps and if this solves your issue mark it as best answer!
Regards,
Tyler
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
That was it TYler.  Just had to update the assert value to 2 and not 100% coverage.
Thank you.
P