I've been writing test classes for already existing code that never had any tests created for it before. Everything seems to be working although I cannot cover one of the lines. For example code here:
global void execute(Database.BatchableContext BC, List<sObject> scope) {
Set<Id> scopeIds = new Map<Id, Product_Bundle__c>((List<Product_Bundle__c>)scope).keyset();
Map<Id, Set<String>> bundleToCurrencies = new Map<Id, Set<String>>();
for (PriceBookEntry pbEntry: [SELECT Id, CurrencyIsoCode, Product2.Product_Bundle__c FROM PriceBookEntry WHERE Product2.Product_Bundle__c IN: scopeIds AND PriceBook2.Name = 'NPD Price Book']){
System.debug('Batch debug pbentry' + pbentry); <-- returns a record.
Id bundleId = pbEntry.Product2.Product_Bundle__c;
System.debug('bundleid is:: ' + bundleId); <-- returns a id
System.debug('bundleToCurrencies map is::' + bundleToCurrencies); <-- returns null
if (bundleToCurrencies.containsKey(bundleId)){
bundleToCurrencies.get(bundleId).add(pbEntry.CurrencyIsoCode);
}
else {
bundleToCurrencies.put(bundleId, new Set<String>{pbEntry.CurrencyIsoCode});
}
}
I cannot seem to cover the if line: if (bundleToCurrencies.containsKey(bundleId))
I've tried to insert test records that would meet the queries criteria but the line never gets covered. In looking at what was written it seems like it will never be true as the code does not add any records to the map to check against. The map gets created, a query happens, then an id off that query is checked against the map.
My question is if that if will never be true and the code itself works then can I just remove the if else and put the id and currency code into the map?