You need to sign in to do that
Don't have an account?
SFDC 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
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; } }
And pass actual response Value