0

With Given below code Successfully implemented HttpCalloutMock . Method contains return type as HTTPResponse , i tried to run the test class for those methods that works perfect.Test method for getpermissionresponse(String accesstoken) covered all lines but how to cover in test coverage of getpermission() ? how to write test method that contains API calling method which is called. as i write Testmethod

in class i written methods like :-

public class facebook_API
{
  public static HttpResponse getpermissionresponse(String accesstoken) //covered in test class
  {
    Http http =new Http();
    String flagpermit ='true';
    HttpRequest req =new HttpRequest();
    req.setEndpoint('https://graph.facebook.com/me/permissions?access_token='+accesstoken);         
    req.setmethod('GET');
    return http.send(req);
  }
 public static void getpermission(String accesstoken) // how to cover in test coverage ? 
 {
    if((accesstoken!='')&&(accesstoken!=null))
    {
       HttpResponse res;
       res= facebook_API.getpermissionresponse(accesstoken);

       if(res.getStatusCode()==200)
       {
           list<facebookpermission.Data>  objpermission = new list<facebookpermission.Data>();
           MBT_SFFB.facebookpermission objfacebookuserpermission = new MBT_SFFB.facebookpermission();
           objfacebookuserpermission=MBT_SFFB.facebookpermission.parse(res.getBody());
       // insert statements for permissions.           
       }
    }
 }
 }

In Test class

 @isTest static void testgetpermissions()
{
   facebook_API.getpermission('EAACEdEose0dddfsdreZBSMh2KPTzhDN2Hrp2vZCnZAYhKyxEuMb5B1QJ3r2ZCaWcwgRUsz9D8bNa10XSuhkAPHYEiHUtRzeheubZAqWzetB5jsGBACsM9IuZB4qsQNteDXIjANtJZCSGIijNyk6LDxYlQHVTpkBYFX4ZD'); 
}
@isTest static void testgetpermission() {

  String json = '{'+
    '  \"data\": ['+
    '    {'+
    '      \"permission\": \"user_friends\",'+
    '      \"status\": \"granted\"'+
    '    },'+        
    '  ]'+
    '}';

    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorforpermission());
    HttpResponse res = facebook_API.getpermissionresponse('EAACEdEose0dddfsdreZBSMh2KPTzhDN2Hrp2vZCnZAYhKyxEuMb5B1QJ3r2ZCaWcwgRUsz9D8bNa10XSuhkAPHYEiHUtRzeheubZAqWzetB5jsGBACsM9IuZB4qsQNteDXIjANtJZCSGIijNyk6LDxYlQHVTpkBYFX4ZD');

    // Verify response received contains fake values
    String contentType = res.getHeader('Content-Type');
    System.assert(contentType == 'application/json');
    String actualValue = res.getBody();
    String expectedValue = json;
    System.assertEquals(actualValue, expectedValue);
    System.assertEquals(200, res.getStatusCode());
}

Mock Class

 @isTest
global class MockHttpResponseGeneratorforpermission implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {

    // Optionally, only send a mock response for a specific endpoint
    // and method.
    String json = '{'+
'  \"data\": ['+
'    {'+
'      \"permission\": \"user_friends\",'+
'      \"status\": \"granted\"'+
'    },'+        
'  ]'+
'}';
    System.assertEquals('https://graph.facebook.com/me/permissions?access_token=EAACEdEose0dddfsdreZBSMh2KPTzhDN2Hrp2vZCnZAYhKyxEuMb5B1QJ3r2ZCaWcwgRUsz9D8bNa10XSuhkAPHYEiHUtRzeheubZAqWzetB5jsGBACsM9IuZB4qsQNteDXIjANtJZCSGIijNyk6LDxYlQHVTpkBYFX4ZD', req.getEndpoint());
    System.assertEquals('GET', req.getMethod());
    // Create a fake response
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody(json);
    res.setStatusCode(200);
    return res;

}

}

Not Covering in coverage All lines after the below line :-

 if(res.getStatusCode()==200)
3
  • Can you post your mockapi callout class also?
    – Reshma
    Feb 15, 2017 at 9:41
  • Just use the mock class also in your testgetpermissions method. Feb 15, 2017 at 9:43
  • Please look at Edited Question. @Reshma Feb 15, 2017 at 9:47

1 Answer 1

0

Can you try this test method in your test class. You need to call method getpermission instead of getpermissionresponse. It will cover both the methods.

@isTest static void testgetpermission() {

    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorforpermission());

    facebook_API.getpermission('test');

    //Assert what you are doing after getting response
}
5
  • But my getpermission() return type must be void. i could not change its return type as HttpResponse Feb 15, 2017 at 10:07
  • @InnodelSalesforce I have updated my code. You need to assert what you are doing with response you got after api callout.
    – Reshma
    Feb 15, 2017 at 10:15
  • It is calling one another Method that call API with Different ENDPOINT Feb 15, 2017 at 12:01
  • @InnodelSalesforce you need set endpoint in mock class using req.setEndpoint('your end point url');
    – Reshma
    Feb 15, 2017 at 12:07
  • in case of test class :- how to write testcode for 2 API response using mock class ?? as i found in Docs there is one method of HttpCalloutMock Interface that is Respond() that returns only one HttpResponse at a time. My case is one API call success makes another API call in single method.. Feb 16, 2017 at 7:47

You must log in to answer this question.

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