how to cover map in test class

Presumably you have multicurrency enabled in your org? If so then you insert CurrencyType objects in your unit test and then call the method. (It is possible that existing CurrencyType objects are automatically visible to your test but that type is not listed in Isolation of Test Data from Organization Data in Unit Tests.) See e.g. An Introduction to Apex Code Test Methods for general points about tests.

Commented Mar 21, 2016 at 10:20

as an aside getCurrencyTypeByISOCode() should cache its results in a static variable so every time it is called in a transaction, it doesn't have to requery the currency rates and burn up valuable soql; same for isMultiCurrencyorg()

Commented Mar 21, 2016 at 17:09

1 Answer 1

First of all, we need to identity which kind of Map we are using in our class.

There are two types of Map which we can cover using test class.

1) Private Map - Map is declared as Private or inside a method which is not accessible in Test Class. We can not pass values by test class.

In order to cover private map, first we need to find the line where we put values into map in our class like mapName.put(). We need to focus only on this line. If put() method of Map is under any IF condition or For loop. Please make sure that IF condition and For loop is covered first then only map will be covered.

2) Public Map - Map is declared as Public which can be accessible in Test Class. We can pass values by Test class.

In order to cover public map, we can simply access this map in our test class and put values directly it. It can also be a parameterized map which is passed inside a method like

Public Void Method_Name( Map < Id, Account >acc );

We can put values while calling this method in our test class.

In your scenario, Map "mapCurrencyTypeByISOCode" is private as decalred inside method and falling under for loop.

So please make sure the SOQL query is returning some values so that execution can come inside for loop and map will be covered.

You need to create test data for CurrencyType object, please follow below link for more details about currencyType