0

    responseSize = ParsedResponse.results.size();
    if(tResults.keySet().size()>0){                      
    for(Results re : ParsedResponse.results){            
       customObj tLocs = new customObj(Building_Name__c=re.buildingname,                                                                                  
                                       Street_Name__c = re.streetName,                                            
                                       country__c = re.country,                                                                                 
                                       state__c = re.state,                                                 
                                       city__c = re.city,                                                                                  
                                       Zip_Code__c = re.postal,                                                
                                       external_id__c = re.externalkey                                                                              
       );
               
    public class GetResponseBodyClass {       
            public List<Results> results;
    } 
    
    public class Results {
            public String street;
            public String country;
            public String state;
            public String city;
            public String postal;
    }    

Below is the test class-


    @isTest
    private class apiSync_Test{
        
        @isTest
        private static void insertTls(){
            apiSync.GetResponseBodyClass tl = new apiSync.GetResponseBodyClass();        
            HttpResponse resp = new HttpResponse();
            resp.setHeader('Content-Type', 'application/json');  
            resp.setStatusCode(200);
            resp.setBody(apiSync_Test.getResponse());  
            QueueHttpMock mock = new QueueHttpMock();
            mock.addResponse(resp);
            Test.setMock(HttpCalloutMock.class,mock);        
        }
        
        public static string getResponse()
        {
            apiSync.GetResponseBodyClass rbc = new apiSync.GetResponseBodyClass();
            rbc.total = 500;
            rbc.page = 1;   
           // rbc.results = ({buildingname='TEST';street='ONE';uniqueKey='65776'},{buildingname='TEST';street='TWO';uniqueKey='57767'});
            return JSON.serialize(rbc);
        }      
        
    }

3
  • 1
    Your test class is not a test class - it is not declared as such, and it does no assertions. Try going through this first: ( How do I write an Apex unit test? ). Then, after writing a test class and trying to get it to work you are still stuck, come back and edit your question to include 1) your updated test class, 2) any other updated code, 3) exact errors and/or unexpected results. Good luck!
    – Moonpie
    Commented Sep 16, 2021 at 22:07
  • It's very unclear what you are asking. Are you struggling with how to populate the List<Results> results in your test class? Please edit to clarify.
    – David Reed
    Commented Sep 17, 2021 at 1:25
  • Yes. But I m not sure how I can populate. I m trying to create some test data in rbc.results in getResponse3() method.
    – Kushi tin
    Commented Sep 17, 2021 at 2:31

1 Answer 1

2

Just create a list of wrapper with your dummy data as shown below

List<apiSync.Results> results = new List<apiSync.Results>();

apiSync.Results res1 = new apiSync.Results();
res1.buildingname = 'test';
res1.street = 'test';
...
results.add(res1);

/* repeat for multiple result entries */

...
rbc.results = results;
return JSON.serialize(rbc);
0

You must log in to answer this question.

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