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
Hitesh AlgamwarHitesh Algamwar 

Test class for below HTTPRequest code

global class FacebookEndUrl {
    global  static void postingFacebookPage()
    {
        
        Facebook__c objData = Facebook__c.getValues('Facebook Data');
        String accessToken = objData.Access_Token__c;
        String message =objData.Message__c;
        String groupId =objData.Page_Id__c;
        
        list<facebook_Post__c> lstData= [select id , status__c , name, Event_Date_time__c from facebook_post__c];
        
        for(facebook_Post__c obj :lstData)
        {
            if(obj.status__c =='Approved')
            {
                
                String url ='https://graph.facebook.com/v15.0/'+groupId+'/feed';
                
                HttpRequest req = new HttpRequest();               
                req.setEndpoint(url);
                req.setMethod('POST');
                req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
                req.setBody('access_token='+accessToken+'&message='+message);
                Http http = new Http();
                HTTPResponse res = http.send(req);
                
                if(res.getStatusCode()==200)
                {
                    
                    system.debug('Success Response :- '+res.getBody());
                }
                else {
                    system.debug('Failed Response :- '+res.getBody());
                }
                
                
                
            }
            
        }
        
        
    }
    
    
}
SwethaSwetha (Salesforce Developers) 
HI Hitesh,
You can use the code snippet from below links to get started
https://salesforce.stackexchange.com/questions/160413/apex-test-class-for-web-api-call . The idea here is to use
built-in WebServiceMock interface and the Test.setMock methods to receive fake responses in a test method and achieve code coverage.

More on using Mocks and Stub Objects:https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/mock-stub-objects

Apex Test Class Examples for Web Services: https://jayakrishnasfdc.wordpress.com/2021/01/02/apex-test-class-examples-for-web-services/

If this information helps, please mark the answer as best. Thank you
Hitesh AlgamwarHitesh Algamwar
Thank you @swetha. Will go through it and update you.
 
Hitesh AlgamwarHitesh Algamwar

Hi Swetha, I tried using the mock method but still code coverage is not coming for the main class. 

Can you please help to write that. 

@isTest
global class FacebookEndUrlMockImpl implements HttpCalloutMock {
        global HTTPResponse respond(HTTPRequest req){
            try{
            //if(req.getEndpoint()=='https://graph.facebook.com/v15.0/558920759441232/feed?message=Update%2013%20of%20Jan&access_token=EAAPOQFcHj8MBAGhWhWfRfnbPO595G0E6FV48CNJcstJXiSLe25YZAYFF1DaYMMo16T03ecYDKzn5UVi0sazuisz5fMNVqtNckx3OhjLFjm5C2PDKDXRPBySizOn5RmKVemvVfoX6IZCMNQhb4z0KZCwA7pwWvIAMCNkSZAEacvyxqDN1yGjY'){
                HttpResponse res= new HttpResponse();
                res.setHeader('Content-Type','application/json');
                res.setBody('{"Name":"Test Name"}');
                res.setStatusCode(200);
                return res;
           // }
        }catch(Exception ex){
                System.debug('ERROR Message>>>'+ex.getMessage()+' Error Line Number>>> '+ex.getLineNumber());
        }
            return null;
         }

}

______________________________________

@isTest
public class FacebookEndUrlTest {
    
    @isTest static void testPostingFacebookPage() {
    
        
         Test.setMock(HttpCalloutMock.Class, new FacebookEndUrlMockImpl());
          
    }
    
}