1

This question is sort of a continuation to this question. I have this method:

public static void sendMail(List<Object> selectedRows) {
     if (selectedRows.size() > 0) {
          for (Object obj : selectedRows) {
               Map<Object, Object> mapJson = (Map<Object, Object>)obj;
               String title = (String)mapJson.get('title');
               String link = (String)mapJson.get('link');
          }
     }
}

I wanted to mock selectedRows list for testing. I did this:

List<Object> selectedRows = (List<Object>)JSON.deserializeUntyped('[{"title":"Title1","link":"https://www.google.com"}]');

based on the answer. Also, I converted Map<Object, Object> to Map<String, Object> based on the suggestion in the answer. But, after that when I run the code, i am getting this error: Invalid conversion from runtime type Map<ANY,ANY> to Map<String,ANY>. So, I changed it back to Map<Object, Object>. Then, the test code throws this error: Invalid conversion from runtime type Map<String,ANY> to Map<ANY,ANY>.

So, the current situation is either the code works or the test works, but not both. How can I solve this issue?

1

This shall work perfectly with above method.

@isTest
    private static void testSendMail(){
        List<Map<Object, Object>> jsonMap = new List<Map<Object, Object>>();
        Map<Object, Object> tempMap = new Map<Object, Object>();
        tempMap.put('title'  , 'Title1');
        tempMap.put('link'  , 'https://www.google.com');
        jsonMap.add(tempMap);
        List<Object> selectedRows = (list<Object>)jsonMap;
        Q_313948.sendMail(selectedRows); //class in which sendMail is present
   }

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.