[SalesForce] Salesforce unit test method to test Batch start

Is that possible to create and validate the below start method

public class testBatch implements Database.Batchable<SObject>
{   
    public Database.QueryLocator start(Database.BatchableContext batchableContextVar)
    {  
        return Database.getQueryLocator('SELECT Id,Name FROM User');             
    }

    ...

what i want is to create test class and assert the below

List<Sobject> sObjecList = testBatch.start(BC)//struck here to create BC
System.Assert(sObjectList.size()>0);

Just want to check this is possible.

Best Answer

Generally, you use Test.startTest() and Test.stopTest() to test batchable classes, like so:

Test.startTest();
Database.executeBatch(new testBatch());
Test.stopTest();

The only drawback is that we're limited to a single batch using this method, so that's usually why you'll see either a custom query or custom limit:

global Database.QueryLocator start(Database.BatchableContext context) {
    return Database.getQueryLocator(
        [SELECT Id FROM User LIMIT :Test.isRunningTest()?1:50000000]
    );
}

Some people prefer to pass in a SOQL string variable, but that's just a bad idea. You should prefer inline static query statements when possible.