function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Muskan PradhanMuskan Pradhan 

System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> error in test class

Hi,

I'm getting System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> error in Test Class. The response body that I have created is res.setBody('[{"attributes": {"type": "Account"},"name": "test"}]');
 This is the line of the main class where test class throws error.
Map<String, object> o = (Map<String, object>) JSON.deserializeUntyped(input);
Any help would be appreciated.
Thanks!!
Khan AnasKhan Anas (Salesforce Developers) 
Hi Muskan,

Greetings to you!

You are getting List with JSON, so you need to parse it into List. Then you can parse it into key-value pair.
 
List<Object> results = (List<Object>) JSON.deserializeUntyped(input);
for (Object obj: results) {
    System.debug((Map<String, Object>)obj);

Please refer to the below links which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/17549/invalid-conversion-from-runtime-type-listany-to-mapstring-any

https://salesforce.stackexchange.com/questions/234884/callout-error-invalid-conversion-from-runtime-type-listany-to-mapstring-any

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Muskan PradhanMuskan Pradhan
Hi Khan

Thanks for the reply!
However, I cannot make any modifications in the main class. Will have to change the test response or modify my test Class.

Thanks
MKRMKR
Hi,

Try this for setting the input:
res.setBody('{"attributes": {"type": "Account"},"name": "test"}');

Regards,
MKR​​​​​​​
Muskan PradhanMuskan Pradhan
I am getting System.StringException: Starting position out of bounds: -1 error after changing the response body.
MKRMKR
Hi,

Is it the same row that is causing the error?

Regards,
MKR