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
Veerendar AellaVeerendar Aella 

need test class for apex class Import Json

Hi All,

I have an Apex class for importing Json file into the salesforce org, I need a test for this apex code. Please help.

Apex Code: 
public class ParserJson_New{
    

    public Blob myfile{get;set;}

    public ParserJson_New(){

        reports =  new fromJSON();

    }

     

    public fromJSON reports {get;set;}
   
    public void doUpload() {
        
        System.debug('myfile'+myfile.toString());

        reports =  (fromJSON) System.JSON.deserialize(myfile.toString(), fromJSON.class);
        
         List<cls_record> rec = reports.leaddata.record ;

        List<Lead> leadList = new List<Lead>();

        for(cls_record c :rec){

            leadList.add( new Lead( FirstName =c.FirstName ,LastName = c.LastName , Phone =c.Phone , 
                                   Email =c.Email, Company =c.Company, Fax =c.Fax, Description =c.Description
                                  , Industry =c.Industry, Status =c.Status, Website =c.Website, LeadSource =c.LeadSource));

        }

        insert leadList;

    }

    public class fromJSON{

        public cls_leaddata leaddata{get;set;}

    }

    public class cls_leaddata {

        public cls_record[] record{get;set;}

    }

    public  class cls_record {

        public String FirstName{get;set;}   
        public String LastName{get;set;}    
        public String Phone{get;set;}   
        public String Email{get;set;}   
        public String Company{get;set;}
        public String Fax{get;set;}
        public String Description{get;set;}
        public String Industry{get;set;}
         public String Status{get;set;}
         public String Website{get;set;}
         public String LeadSource{get;set;}
    }
}

Json File: 

{

    "leaddata": {

        "record": [

            {

                "FirstName": "Zepra",

                "LastName": "Json",

                "Phone": "8008237678",

                "Email": "Zepra@gmail.com",

        "Company": "Zepra Inc",
        
        "Fax": "143215",

        "Industry": "Zepra",

        "Description": "This is my Zepra Json",

        "Status": "Working -Contacted",

        "Website": "WWW.ZepraJson.com",
    
        "LeadSource": "Web"
            }

            
        ]

    }

}
 
Best Answer chosen by Veerendar Aella
Raj VakatiRaj Vakati
Try this
 
@istest
public class ImportAccountsJSONTest{
    public static testmethod void helptest(){
        String hresult = '{"leaddata": {'+
            '"record": ['+
            '  {'+
            '   "FirstName": "Zepra",'+
            '"LastName": "Json",'+
            ' "Phone": "8008237678",'+
            '"Email": "Zepra@gmail.com",'+
            '"Company": "Zepra Inc",'+
            '"Fax": "143215",'+
            '"Industry": "Zepra",'+
            '"Description": "This is my Zepra Json",'+
            '"Status": "Working -Contacted",'+
            ' "Website": "WWW.ZepraJson.com",'+
            '"LeadSource": "Web"}]}} ';
        
        ParserJson_New imp = new ParserJson_New() ;
        imp.myfile  = Blob.valueOf(hresult) ; 
        imp.doUpload();
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
@istest
public class ImportAccountsJSONTest{
    public static testmethod void helptest(){
        String hresult = '{"leaddata": {'+
            '"record": ['+
            '  {'+
            '   "FirstName": "Zepra",'+
            '"LastName": "Json",'+
            ' "Phone": "8008237678",'+
            '"Email": "Zepra@gmail.com",'+
            '"Company": "Zepra Inc",'+
            '"Fax": "143215",'+
            '"Industry": "Zepra",'+
            '"Description": "This is my Zepra Json",'+
            '"Status": "Working -Contacted",'+
            ' "Website": "WWW.ZepraJson.com",'+
            '"LeadSource": "Web"}]}} ';
        
        ParserJson_New imp = new ParserJson_New() ;
        imp.myfile  = Blob.valueOf(hresult) ; 
        imp.doUpload();
        
    }
}

 
This was selected as the best answer
Veerendar AellaVeerendar Aella
Cool, Thanks Raj once again.