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
guptach45guptach45 

Test class for Batch class. I have 70% coverage. Please help me to increase code coverage.

global class ItalyPriceBookIntegrationBatch implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        Datetime lastOneHour = Datetime.now().addMinutes(-60);
        string finalQuery = 'Select Id, Sales_Org__c, Product_SKU_Calculated__c from Product2 where (NOT Material_Master_ID__c like \'' +'%SEP%' + '\')' 
            +' and LastModifiedDate >= ' +string.valueOf(lastOneHour.Date())+'T'+string.valueOf(lastOneHour.Time());
        system.debug('Final Query --> ' +finalQuery);
        return Database.getQueryLocator(finalQuery);
    }
    
    global void execute(Database.BatchableContext BC,List<Product2> scope){
        set<Id> prodIdSet = new set<Id>();
        List<PriceBookEntry> newPBEList = new List<PriceBookEntry>();
        List<PriceBookEntry> updatePBEList = new List<PriceBookEntry>();
        map<Id,PriceBookEntry> pbeProdMap = new map<Id,PriceBookEntry>();
        string it01PBid = System.Label.IT01_PriceBookId;
        for(Product2 prod : scope){
            prodIdSet.add(prod.Id);
        }
        System.debug('the Price Book Entries are -->'+prodIdSet);
        for(PriceBookEntry pbeList : [Select Id,Name,Product2Id,IsActive from PriceBookEntry where Pricebook2Id =:it01PBid and Product2Id In :prodIdSet]){
            pbeProdMap.put(pbeList.Product2Id,pbeList);
        }
        System.debug('the Price Book Entries are -->'+pbeProdMap);
        for(Product2 prodIT : scope){
            if(prodIT.Sales_Org__c == 'IT01'){
                if(pbeProdMap.get(prodIT.Id)!= Null){
                    PriceBookEntry oldPBE = pbeProdMap.get(prodIT.Id);
                    System.debug('the PBE is -->'+oldPBE.Id+oldPBE.Name);
                    if(!oldPBE.IsActive){
                        oldPBE.IsActive = True;
                        
                        updatePBEList.add(oldPBE);
                    }
                    
                }else {
                    PriceBookEntry newPBE = new PriceBookEntry();
                    newPBE.Pricebook2Id = System.Label.IT01_PriceBookId;
                    newPBE.Product2Id = prodIT.Id;
                    newPBE.IsActive = True;
                    newPBE.PBE_External_Id__c = 'EPC-IT01-'+prodIT.Product_SKU_Calculated__c;
                    newPBE.UnitPrice = 0.00;
                    newPBE.CurrencyIsoCode = 'EUR';
                    
                    newPBEList.add(newPBE);
                }
            }else {
                if(pbeProdMap.get(prodIT.Id)!= Null){
                    PriceBookEntry oldPBE = pbeProdMap.get(prodIT.Id);
                    System.debug('the PBE is -->'+oldPBE.Id+oldPBE.Name);
                    if(oldPBE.IsActive){
                        oldPBE.IsActive = False;
                        
                        updatePBEList.add(oldPBE);
                    }
                }
            }
        }
        system.debug('The update list is -->'+updatePBEList.size());
        if(updatePBEList.size() > 0){
            Database.SaveResult[] oldPBE = Database.update(updatePBEList,false);
            for(Database.SaveResult opbe:oldPBE){
                if(!opbe.isSuccess()){
                    system.debug(' Error-->'+opbe.getErrors());        
                }
            }
        }
        system.debug('The new list is -->'+newPBEList.size());
        if(newPBEList.size() > 0){
            Database.SaveResult[] newPBE = Database.insert(newPBEList,false);
            for(Database.SaveResult npbe:newPBE){
                if(!npbe.isSuccess()){
                    system.debug(' Error -->'+npbe.getErrors());        
                }
            }
        }
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
    
}

Test Class--

@isTest
public class ItalyPriceBookIntegrationBatch_Test {
@testsetup
    static void setup(){
        Product2 prodOne =  DivAC_00_TestFactory.getProduct('Test Product');
        prodOne.Sales_Org__c = 'IT01';
        prodOne.Material_Master_ID__c = '12345_EPC';
        insert prodOne;
        Pricebook2 standardPricebook = DivAC_00_TestFactory.getStdPriceBookId();
        Map<id,PricebookEntry> pbeProdMap = new Map<id,PricebookEntry>();
        PricebookEntry pbe = DivAC_00_TestFactory.getPriceBookEntry(String.ValueOf(standardPricebook.Id),String.ValueOf(prodOne.Id),2);
        pbe.Product2Id = prodOne.id;
        pbe.IsActive = true;
        insert pbe;
        pbeProdMap.put(pbe.Product2Id,pbe);
        pbeProdMap.get(prodOne.Id);
        System.debug('the Price Book Entries are -->'+pbeProdMap);
        PriceBookEntry oldPBE = pbeProdMap.get(prodOne.Id);
        
    }
    static testmethod void test(){
        Test.startTest();
        ItalyPriceBookIntegrationBatch ipb = new ItalyPriceBookIntegrationBatch();
        ID batchId = Database.executeBatch(ipb);
         Test.stopTest();
   
    }
}

 
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Follow the methods as mentioned in the above developer blog to increase the code coverage.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
RajandraSRajandraS
Hi There,
Check This code:
@isTest
public class ItalyPriceBookIntegrationBatch_Test {
@testsetup
    static void setup(){
        Product2 prodOne =  DivAC_00_TestFactory.getProduct('Test Product');
        prodOne.Sales_Org__c = 'IT01';
        prodOne.Material_Master_ID__c = '12345_EPC';
        insert prodOne;
        Product2 prodTwo =  DivAC_00_TestFactory.getProduct('Test Product2');
        prodTwo.Sales_Org__c = 'IT02';
        prodTwo.Material_Master_ID__c = '12346_EPC';
        insert prodTwo;

        Pricebook2 standardPricebook = DivAC_00_TestFactory.getStdPriceBookId();
        Map<id,PricebookEntry> pbeProdMap = new Map<id,PricebookEntry>();
        Map<id,PricebookEntry> pbeProdMapTwo = new Map<id,PricebookEntry>();

        PricebookEntry pbe = DivAC_00_TestFactory.getPriceBookEntry(String.ValueOf(standardPricebook.Id),String.ValueOf(prodOne.Id),2);
        pbe.Product2Id = prodOne.id;
        pbe.IsActive = true;
        insert pbe;
PricebookEntry pbe2 = DivAC_00_TestFactory.getPriceBookEntry(String.ValueOf(standardPricebook.Id),String.ValueOf(prodTwo.Id),2);
        pbe2.Product2Id = prodTwo.id;
        pbe2.IsActive = true;
        insert pbe2;

        pbeProdMap.put(pbe.Product2Id,pbe);
        pbeProdMapTwo.put(pbe2.Product2Id,pbe);

        pbeProdMap.get(prodTwo.Id);
        System.debug('the Price Book Entries are -->'+pbeProdMap);
        PriceBookEntry oldPBE = pbeProdMap.get(prodOne.Id);
        
    }
    static testmethod void test(){
        Test.startTest();
        ItalyPriceBookIntegrationBatch ipb = new ItalyPriceBookIntegrationBatch();
        ID batchId = Database.executeBatch(ipb);
         Test.stopTest();
   
    }
}