You need to sign in to do that
Don't have an account?
guptach45
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(); } }
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.
Check This code: