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:

    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);

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!

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.