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
The new LearnerThe new Learner 

Test class for Json method

Hi Experts,

Can anyone help me how to write test class for the below method.

public override void onCommit(){
        
        OM_ManageService manageRemittanceSVC = (OM_ManageRService)this.svc;
        String accJSON = OM_WorkService.getJSONAttribute(manageRemittanceSVC.jsonString, 'AccountInfo');
        Map<String, List<SObject>> accObjects =  OM_WorkService.getSObjects(accJSON);
        Account acc = (Account)accObjects.get('Account')[0];
        
        if(acc!= null){
            upsert acc;
            super.onCommit();
            
            }
            
        Below is the OM_WorkService.getJSONAttribut    
             
             public static String getJSONAttribute(String jsonStr, String attr){
        Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);
        if(m.containsKey(attr))
            return (String)m.get(attr);
        
        return null;
    }
 
Pruthiviraj GanePruthiviraj Gane
Hi
 
@isTest
private class TestClass {

    static testMethod void testMethod() {
       Account a = new Account();
       a.Name = 'Test';
       List<Account> accountList = new List<Account>();
       accountList.add(a);
       Map<String,List<Account>> accountMap = new Map<String,List<Account>>();
       accountMap.put('AccountInfo',accountList);
       String json = JSON.serialize(accountMap);

       OM_ManageService o = new OM_ManageService();
       o.svc.jsonString = json;
       
    }
}

Then you have to call the commit method within the test method. If you can send definition for svc from  OM_ManageService class and the name of the class where onCommit method exists. So I can send you more code regarding this.