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
priyanshu nema 2priyanshu nema 2 

How to write test class for map<object,object>collection object

How to write test class for map<object,object>collection object please have a look the below code
 @AuraEnabled
    public static String createContactRecords(List<Object> lstObjects) {
        
        For(Object obj : lstObjects) {
            Map<Object, Object> mapContact = (Map<Object,Object>)obj;
            
            If(mapContact.get('type') == 'string' || 
               mapContact.get('type') == 'text' || 
               mapContact.get('type') == 'textarea' || 
               mapContact.get('type') == 'url' || 
               mapContact.get('type') == 'phone' || 
               mapContact.get('type') == 'email'
              )  {
                  If(mapContact.get('value') != null && String.valueOf(mapContact.get('value')) != '') {
                      objContact.put(String.valueOf(mapContact.get('field')), String.valueOf(mapContact.get('value')));
                  }
              }
}





test class-
@isTest
public class ContactFormHandler_Test {
    @isTest
    public static void test(){
        Account acc= new Account();
        acc.Name = 'test';
        insert acc;
        contact con = new Contact();
        con.LastName = 'test1';
        insert con;
        List<object> objList = (List<object>)json.deserializeUntyped('[{"field": "FirstName","type": "string", "value": "tedf"},{"field":"LastName", "type":"string", "value":"hgghjh"}]');

ContactFormHandler.createContactRecords(objList);

It show error - System.TypeException: Invalid conversion from runtime type Map<String,ANY> to Map<ANY,ANY> 
     

Best Answer chosen by priyanshu nema 2
Shri RajShri Raj
@isTest
private class TestCreateContactRecords {
    @isTest
    static void testCreateContactRecords() {
        // Prepare test data
        List<Object> lstObjects = new List<Object>();
        Map<Object, Object> mapContact1 = new Map<Object, Object>();
        mapContact1.put('field', 'FirstName');
        mapContact1.put('type', 'string');
        mapContact1.put('value', 'John');
        lstObjects.add(mapContact1);
        
        Map<Object, Object> mapContact2 = new Map<Object, Object>();
        mapContact2.put('field', 'LastName');
        mapContact2.put('type', 'string');
        mapContact2.put('value', 'Doe');
        lstObjects.add(mapContact2);
        
        Map<Object, Object> mapContact3 = new Map<Object, Object>();
        mapContact3.put('field', 'Email');
        mapContact3.put('type', 'email');
        mapContact3.put('value', 'john.doe@example.com');
        lstObjects.add(mapContact3);
        
        // Call the method to test
        String result = YourClassName.createContactRecords(lstObjects);
        
        // Assert the result
        System.assertEquals('Success', result, 'The result should be "Success"');
    }
}