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
SFDC BUGSFDC BUG 

HTTP GET Method Test class

Hi All 

How can i improve my code coverage of my HTTP get method contoller...I have written but ist 73%and unable to improve
 
public with sharing class Popular {

    public Popular(ApexPages.StandardController controller) {

    }

    Public List< Object> mainitems {get; set;}
    Public Map < String, Object > mapitems {get; set;}
    Public List<Map < String, Object >> listmapitems {get; set;}
    public Popular() {

    }


    public PageReference webservcie(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://retail.testlabs.com/v1/popular?client_id=Test&rails=sellers');
        //request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');
        
        string autho = 'Bearer '+userInfo.getSessionId();
        request.setHeader('Authorization', autho);
        
        HttpResponse response = http.send(request);
        String jsonBody = response.getBody();
        List < Object > items = (List < Object > ) JSON.deserializeUntyped(jsonBody);
        system.debug('****' + items);
        for (Object o: items) {
            Map < String, Object > maprec = (Map < String, Object > ) o;
            String pgnu = (String) maprec.get('rail_name');
            system.debug('&&&&pgnu' + pgnu);
            mainitems = (List < Object > ) maprec.get('items');
            system.debug('&&&&items' + mainitems);
        }
        for (Object m: mainitems) {
            mapitems = (Map < String, Object > ) m;
            String strbrand= (String) mapitems.get('brand');
            system.debug('##&&items' + strbrand);
        }
        return null;
    }
   
}
 
@isTest
private class Popular_Test{
    static testMethod void test_webservcie_UseCase1(){
        
      
        
    PageReference pageRef = Page.Popular;
    
    Test.setCurrentPage(pageRef);
   
    if (Test.isRunningTest()) {  
       Test.setMock(HttpCalloutMock.class, new Popular_Mock());
       
       
       Popular obj01 = new Popular();
       obj01.webservcie();
       }
   
    
   
       
   
    
    }
}
=======================================
@isTest
global class Popular_Mock implements HttpCalloutMock{
  // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
       System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        String body = '[{ "item_id": "...................: "string"}]';//Pass the json 
        res.setBody(body);
        res.setHeader('Content-Type', 'application/xml');
        res.setStatusCode(200);
        return res;
    }
}

 
Aslam ChaudharyAslam Chaudhary
It seems you are not creating correct sample return GET data. Which line of code is not covering?
Amit Chaudhary 8Amit Chaudhary 8
Try to update below line
String body = '[{ "item_id": "...................: "string"}]';//Pass the json

And pass actual response Value