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
venkat narayan 7venkat narayan 7 

I have written http callout get method for integration of facebook with salesforce. Can anyone tell how to write the test classes for this code

Wrapper class:
public class facebookwrapper {
public string name {set;get;}
public string firstname {set;get;}
public string lastname {set;get;}
}
Apex class:
public class FbClass { public string appid {set;get;} public string appsecret {set;get;} public string redirecturl {set;get;} public string code {set;get;} public string jsonString {set;get;} public string accessToken {set;get;} PUBLIC STRING history {set;get;} public string selfdetails {set;get;} public list<facebookwrapper> fbwrap {set;get;} public FbClass(){ appid = '922161527964144'; appsecret = '<it"s a secret to everyone>'; redirecturl = 'https://c.ap5.visual.force.com/apex/FbClass'; fbwrap=new list<facebookwrapper> (); } public PageReference fbaction(){ HttpRequest req=new HttpRequest(); string endpoint='https://www.facebook.com/dialog/oauth?'; endpoint = endpoint+'client_id='+appid; endpoint = endpoint+'&redirect_uri='+redirecturl; endpoint = endpoint+'&scope=user_friends,ads_read,pages_manage_cta,user_videos'; endpoint = endpoint+'&response_type=code'; endpoint = endpoint+'&state=Venkat'; req.setEndpoint(endpoint); req.setMethod('GET'); HttpResponse res = new HttpResponse(); http h = new Http(); res = h.send(req); // readCode(); PageReference p=new PageReference(endpoint); return p; } public void code1(){ code = apexpages.currentpage().getparameters().get('code'); } public void requestToken(){ String url ='https://graph.facebook.com/oauth/access_token?'; url = url+'client_id='+appid; url = url+'&redirect_uri='+redirecturl; url = url+'&client_secret='+appsecret; url = url+'&code='+code; Http p = new Http(); HttpRequest request=new HttpRequest(); request.setMethod('GET'); request.setEndPoint(url); HttpResponse response = p.send(request); // once your getting the response for yor request jsonString = response.getBody(); System.JSONParser js = JSON.createParser(jsonString); system.debug('In Access Token method'+jsonString); while(js.nextToken() != null){ if(js.getText() == 'access_token'){ js.nextToken(); accessToken = js.getText(); } } } public void gethistory(){ HttpRequest req=new HttpRequest(); req.setMethod('GET'); //string idd = 'AaJ31OBeGO-0suOHezvMaf_p_RdgIN4lIg4x3es3DSXplEIisSaEXzpGZnpuuy-GtqSfCN6fnjmXx1iPVs5bIaBE-qvYZNCxghWaJdYvSOovrQ'; // req.setEndpoint('https://graph.facebook.com/v2.11/work-experience-idd HTTP/1.1'); // req.setEndpoint('https://graph.facebook.com/2034133706611611/statuses'); //req.setEndpoint('https://graph.facebook.com/v2.11/friend-list-id'); req.setEndpoint('https://graph.facebook.com/me?fields=work,education'); req.setHeader('Authorization','Bearer '+accessToken); HttpResponse res = null; http h = new Http(); res = h.send(req); history=res.getbody(); system.debug( 'hiiiiiiiiiiiiiiiii'+res.getbody()); // System.JSONParser jp=JSON.createParser(history); } public void datagetting(){ HttpRequest req =new HttpRequest(); req.setMethod('GET'); string url = 'https://graph.facebook.com/me?fields=taggable_friends{name,first_name,last_name}'; req.setEndpoint(url); req.setHeader('Authorization','Bearer '+accessToken); HttpResponse res = new HttpResponse(); Http h = new Http(); res = h.send(req); system.debug( 'hiiiiiiiiiiiiiiiii'+res.getbody()); selfdetails = res.getbody(); System.JSONParser js = JSON.createParser(selfdetails); while(js.nextToken() != null){ string name1; string firstname; string lastname; if(js.getText() == 'name'){ facebookwrapper fb=new facebookwrapper(); js.nextToken(); name1 = js.getText(); system.debug('Namessss'+name1); fb.name = name1;
system.debug('Nameeee:'+fb); js.nextToken();
if(js.getText() == 'first_name'){ js.nextToken();
firstname = js.getText();
system.debug('Firstnamessssssss:'+firstname);
fb.firstname = firstname; system.debug('Firstnames11111111111111111111:'+fb);
VF Page: 
<apex:page controller="FbClass"> <apex:form > <apex:pageblock > <apex:commandButton action="{!fbaction}" value="click" /> <apex:commandButton action="{!code1}" value="code" /> {!code} <apex:commandButton action="{!requestToken}" value="Token" /> {!accessToken} <apex:commandButton action="{!gethistory}" value="history" /> {!history} <apex:commandButton action="{!datagetting}" value="Data" /> {!selfdetails} </apex:pageblock> <apex:pageblock > <apex:pageblocktable value="{!fbwrap}" var="x"> <apex:column value="{!x.name}" headerValue="Names" /> <apex:column headerValue="FirstName" value="{!x.firstname}"/> <apex:column headerValue="Lastnames" value="{!x.lastname}" /> </apex:pageblocktable> </apex:pageblock> </apex:form> </apex:page>
system.debug('FaceBookWrapper:'+fbwrap); js.nextToken();
if(js.getText() == 'last_name'){ js.nextToken(); lastname = js.getText(); fb.lastname = lastname; fbwrap.add(fb);
}
}
}
}
}
}
Vinod ChoudharyVinod Choudhary
Hi,

You need to create MockHttpResponse  Class for this. in your response you can send whatever you need to run your class data.

Example Code
@isTest
global class MockHttpResponsecls implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
               
       // System.assertEquals('http://api.domain.com/foo/bar', req.getEndpoint());
        //System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(
           '{'+
              '"id": "SOMETHING",'+
              // All the Values that you need to pass Writte Here In Jesaon Formate
            '}');
        

       
        return res;
    }
    
    
}
On your test class use:
Test.setMock(HttpCalloutMock.class, new MockHttpResponsecls());
Keep in mind the Mock Class should be used before all DML operations in your test class.

Hope this Will help you.

Thanks
Vinod