0

I'm writing test class for one of my method and getting this error: Method:

public String onLoadLog(String loaddata) {
         Map<String, Object> LogData = (Map<String, Object>) JSON.deserializeUntyped(loaddata);
         Map<String, Object> interact = ( Map<String, Object>) LogData.get('interact');
         Map<String, Object> call = ( Map<String, Object>) LogData.get('call');
         
         String starttime = (String) interact.get('start');
         DateTime dt = DateTime.ValueOfGmt(start.replace('T',' ').left(start.length()-5));
         call.put('subject','Call ' + dt.format('dd/MM/yyyy, HH:mm:ss'));
         Boolean isConnected = (Boolean) interact.get('isConnected');
         String LogId = '';
         if (isConnected) {
             Event t = (Event) JSON.deserialize(JSON.serialize(call), Event.class);
             upsert t;
             LogId = t.Id;
         }
         return LogId; 
    }

Test Class:

@IsTest
    static void TestMethod(){
        
        String payload = '{"interact": [{"Call": [{"start": [{"s": "a", "i": 1}], "start": 1}], "start": 1}]}';
        Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(payload);
        List<Map<String, Object>> data = new List<Map<String, Object>>();
        for(Object instance : (List<Object>)deserialized.get('interaction')){
            data.add((Map<String, Object>)instance);
        }
        
        Class myctiObj = new Class();
        String str = myctiObj.onLoadLog(payload);
        
        }

1 Answer 1

2

Your JSON doesn't match what the code expects. You need to review either your code or your test class. For example, a valid JSON might look something like:

String payload = '{"interact":{"start":"2021-01-01T00:00:00.000Z","isConnected":true},"call":{"Subject":"Test"}}}';

You'll need to fix your JSON to match what the code expects. You can't just put in a random JSON and expect your code to work.

Also, I strongly recommend using an actual JSON object instead of writing your own JSON, it's far easier and less likely to break:

String payload = JSON.serialize(
  new Map<String, Object> {
    'interact' => new Map<String, Object> {
      'start' => Date.now(),
      'isConnected' => true
    },
    'call' => new Event( // sObject syntax is different!
      Subject='Demo'
    )
 });

You must log in to answer this question.

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