1

Here is my complete class and i want to create its test class

public with sharing class MultiCurrencyHandler {

  public static Boolean isMultiCurrencyOrg(){
    try{
      list<SObject> currencyTypes = Database.query('SELECT IsoCode, ConversionRate FROM CurrencyType limit 1');
    }catch(Exception e){
      // Multi Currency is not supported.
      if(e.getMessage().contains('sObject type \'CurrencyType\' is not supported.'))
        return false;
    }
    return true;
  }

  public static map<String, Double> getCurrencyTypeByISOCode(){
    map<String, Double> mapCurrencyTypeByISOCode = new map<String, Double>();
    for(sObject currType : Database.query( 'SELECT IsoCode, ConversionRate FROM CurrencyType') ){          
          mapCurrencyTypeByISOCode.put( String.valueOf( currType.get('IsoCode') ), Double.valueOf(currType.get('ConversionRate') ) );        
      }

      return mapCurrencyTypeByISOCode;
  }

  public static Double convertCurrencyWith( Double oCurrencyConversionRate, Double nCurrencyConversionRate, Double amount, String nCurrencyType){
      //Convert incoming Opportuntiy Amount into USD...
      amount = amount / oCurrencyConversionRate;

      //convert amount as per new currency. 
      if( nCurrencyType != 'USD' ){
          amount = amount * nCurrencyConversionRate;
      }
      return amount;
  }
}
3
  • You should give more details. At this moment it is absolutely unclear what is the problem. Commented Mar 21, 2016 at 10:01
  • 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.
    – Keith C
    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()
    – cropredy
    Commented Mar 21, 2016 at 17:09

1 Answer 1

3

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

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_currencytype.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .