I have an apex class like that :

public static WrapperFilters manageFilters(Map<String, Object> lines){
   Map<Object, Object> mapLineDetail = new Map<Object, Object>();
    //iterate through inserted lines
    for(String key : lines.keySet()){
        mapLineDetail = (Map<Object, Object>)lines.get(key); //line where the test class fail
        //other code like that :
        if(mapLineDetail.get('ObjectName') == 'Account') {

And here is the test method for this method :

@isTest static void manageFilters_Test() {
    LCR01_AccountList.WrapperLine wrapLine = new LCR01_AccountList.WrapperLine();
    wrapLine.ObjectName = 'Opportunity';
    wrapLine.FieldName = 'Amount';
    wrapLine.Operator = '<';
    wrapLine.Value = '10000';

    system.debug('### wrapLine : ' + wrapLine);

    Map<String, Object> mapFilters = new Map<String, Object>();

    String filter = '{FieldName=AccountId, FieldType=, ObjectName=Opportunity, Operator==, Value=0016E00000TIZUYQA5}';
    Object myObj = filter;
    System.debug('### myObj : ' + myObj);
    mapFilters.put('1', myObj);

    LCR01_AccountList.WrapperFilters wrap = LCR01_AccountList.manageFilters(mapFilters);

The problem is when I run the test class I got this error :

System.TypeException: Invalid conversion from runtime type String to Map

I test many things but I can't get over this error. The test fail at the line indicated in the apex method mapLineDetail = (Map<Object, Object>)lines.get(key);

I specify that the code (apex class) actually works.


You are trying to cast the result of lines.get(key) into a Map<Object, Object>, whereas actually it is of the String type.

Just notice how you initialize it in your test:

Map<String, Object> mapFilters = new Map<String, Object>();
String filter = '{FieldName=AccountId,...}';
Object myObj = filter;
mapFilters.put('1', myObj);

Here myObj is a String

So you should do mapLineDetail = (String)lines.get(key); instead.


When you declare and initialize String filter = '{FieldName=AccountId,...}'; it creates just a string, but in fact your code expects the filter argument to be Map<Object,Object>.

So all you need to do in you test class is update the line String filter = with:

Map<Object,Object> filter = new Map<Object,Object>{'FieldName'=>'AccountId', 'FieldType'=>'datetime', 'ObjectName'=>'Opportunity', 'Operator'=>'=<', 'Value'=>'0016E00000TIZUYQA5'}; 
| improve this answer | |
  • Actually I need mapLineDetail to be a Map<Object, Object> because I use it later in the code... – dou Jul 12 '18 at 8:38
  • then you need to think about altering your implementation. As a quick fix, you can do mapLineDetail = new Map<Object,Object> {<some key>, lines.get(key)}; – Eduard Jul 12 '18 at 8:41
  • If I do that, it works for the unit test, but in real test it does not work anymore – dou Jul 12 '18 at 8:47
  • Maybe because in a real run it is not get executed (e.g. your code never calls the manageFilters method, or the lines parameter is empty)? – Eduard Jul 12 '18 at 8:51
  • 1
    Aha, try this in your test class Map<Object,Object> filter = new Map<Object,Object>{'FieldName'=>'AccountId', 'FieldType'=>'datetime', 'ObjectName'=>'Opportunity', 'Operator'=>'=<', 'Value'=>'0016E00000TIZUYQA5'}; When you create String filter = '{FieldName=AccountId,...}'; it creates just a string, whereas your code expects the filter to be Map<Object,Object> – Eduard Jul 13 '18 at 13:45

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.