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
sumit dsumit d 

test class of http Callout

Hi All,
        how to cover this method in test class?
 public static void addTagsToSocialPost( SocialPost spObj, 
                                            String tags , 
                                            //Map<String, Id> predictionTags,
                                            List<ApiLog__c> apiLogsList){
        // Api call to update  tag to post
        List<ApiLog__c> apiLogList = new List<ApiLog__c>();
        apiLogList.addAll(apiLogsList);
        Map<String, List<ApiLog__c>> accessTokenMap = getAccessToken( spObj
                                                                     //, tags , predictionTags
                                                                     );
        String accessToken  = '';
        for(String at : accessTokenMap.keySet()){
           accessToken = at; 
        }
        apiLogList.addAll(accessTokenMap.get(accessToken));
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint( 'https://api.radian6.com/socialcloud/v1/post/workflow/tags/' + spObj.R6PostId);
        req.setHeader('auth_token', accessToken); 
        req.setHeader('auth_appkey', Label.AppKey);
        Http binding = new Http();
        String tagsToSend = 'tags:'+ tags;
        req.setBody('tags='+tags);
        req.setHeader('content-type','application/x-www-form-urlencoded');
        HttpResponse res;
        res = binding.send( req );
        try{
            res = binding.send( req );
            ApiLog__c apiLog = new ApiLog__c();
            apiLog.EndPoint__c = req.getEndpoint();
            apiLog.ImageURL__c = '';
            //apiLog.Prediction__c = predictionTags.get(tags);
            apiLog.RequestBody__c = req.getBody();
            apiLog.ResponseBody__c = res.getBody();
            apiLog.SocialPost__c = spObj.Id;
            apiLog.StatusCode__c = String.valueOf(res.getStatusCode());
            apiLog.Type__c = 'Social Studio';
            apiLogList.add(apiLog);
        }catch(Exception e){
            ApiLog__c apiLog = new ApiLog__c();
            apiLog.EndPoint__c = req.getEndpoint();
            apiLog.ImageURL__c = '';
            //apiLog.Prediction__c = predictionTags.get(tags);
            apiLog.RequestBody__c = req.getBody();
            apiLog.ResponseBody__c = res.getBody();
            apiLog.SocialPost__c = spObj.Id;
            apiLog.StatusCode__c = String.valueOf(res.getStatusCode());
            apiLog.Type__c = 'Social Studio';
            apiLogList.add(apiLog);
        }
        insert apiLogList;
        
    }
    
Ashish KumarAshish Kumar
You need to create a mock http callout class and call the addTagsToSocialPost in the Test Class.
This link might be useful for you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

Regards,