0

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?

  • does the query return results? – San Tosh Oct 11 '16 at 16:33
  • Yes, I added debugs and the query returns a value, and the bundleId has a value. The map is null however. I'll add to question. – Dan Wooding Oct 11 '16 at 17:15
1

As Fernando Gavinho said you need 2 pricebooks with same Product2.Product_Bundle__c.

What this code does - it groups set of PriceBookEntry.CurrencyIsoCode by Product2 Bundle field:

<Product2.Product_Bundle__c : Set<CurrencyIsoCode>>

Yes, if query returns one record that IF() will be never executed. But if SOQL will return more then 1 pricebookentry with same bundle, in future FOR runs, IF() statement will be true and records will be added to same Key.

| improve this answer | |
0

You need 2 pricebook entries that use the same bundle.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.