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)
testgetpermissions
method.