+ Start a Discussion
shravani milshravani mil 

How to cover Batch class in test class

Below method is not covering in batch class

global void execute(Database.BatchableContext BC, List<SObject> objs) {
        
        system.debug('What is the 1');
        
        List<PriceBookEntry> PbeList=new List<PriceBookEntry >();
        Set<String> prods=new Set<String>();
        Set<String> pbeids=new Set<String>();
       Map<id,SBQQ__ProductOption__c> prodOptions=new Map<id,SBQQ__ProductOption__c>();
        for(PriceBookEntry pbe:(List<PriceBookEntry>)objs){
          prods.add(pbe.Product2Id);
        
        }
          system.debug('prods--'+prods);
        for(SBQQ__ProductOption__c opt:[select id,SBQQ__ConfiguredSKU__c,OptionalSKU_Product_Codes__c,SBQQ__OptionalSKU__r.ProductCode,PKorCK_Flag__c,
                                                              SBQQ__ProductCode__c,SBQQ__ConfiguredSKU__r.ProductCode,SBQQ__OptionalSKU__c,SBQQ__OptionalSKU__r.name from SBQQ__ProductOption__c where SBQQ__OptionalSKU__c IN :prods]){
            if(opt.PKorCK_Flag__c)
         prodOptions.put(opt.SBQQ__ConfiguredSKU__c,opt);
            
        
        }
       system.debug('prodOptions--'+prodOptions);
        system.debug('prodOptions-size-'+prodOptions.keyset().size());
        PackPricingOptionsTriggerHandler handler=new PackPricingOptionsTriggerHandler();
            Map<String,Set<string>> Optionskus=handler.getChildProducts(prodOptions,'Batch_PBE Update');
           system.debug('optionskusize---'+Optionskus.size()+'optionskue--'+Optionskus.Keyset());
           handler.UpdateConfigureSKUPriceValues(Optionskus);
           

}
Amit Singh 1Amit Singh 1
Hello,

Have you inserted test data into your test class as per your requirement and make sure that your start method is returning at least one record from Database.getQueryLocator query and use below syntax in your test class for covering batch class.
Test.stopTest();
Database.executeBatch(new yourBatchClass());
Test.stopTest();

Let me know if this helps :)
Thanks.
Amit Singh
Amit Chaudhary 8Amit Chaudhary 8
Please post your full batch so that we can help you
Please check below post for Batch job test class
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

A sample test class to start
@isTest
public class AccountUpdateBatchJobTest
{
    static testMethod void testMethod1()
    {
       // Create test data according to your Batch job
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstLead.add(acc);
        }
        insert lstAccount;
       
        Test.startTest();

            AccountUpdateBatchJob obj = new AccountUpdateBatchJob(); // Add you Class Name
            DataBase.executeBatch(obj);
           
        Test.stopTest();
    }
}