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

HTTP Callout Request

Hello Fellow Salesforce Developers!

I have a apex class that are not covered and was hoping to get help in writing the codes for it. I'm at 35% and have been working on this for about a week now and cannot figure it out. Any help that you could provide is greatly appreciated, thank you in advance. :) The lines that I need to cover are as followed:

   public void execute(SchedulableContext ctx) {

 HttpRequest req = new HttpRequest();
       String url = INSTANCE + '/services/data/v33.0/actions/standard/metricRefresh';
       req.setHeader('Authorization', 'OAuth ' + sessionId);
       req.setHeader('Force-Instance-Url', INSTANCE); 
       req.setHeader('Force-User-Id', userId);
       req.setHeader('Accept', 'application/json');
       req.setHeader('Content-Type', 'application/json');
       req.setBody('{"inputs":[{"metricId":"' + metricId + '"}]}'); // metric id
       Http http = new Http();
       HTTPResponse res = http.send(req);
Best Answer chosen by JayChen
Chandra Sekhar CH N VChandra Sekhar CH N V
Use test.isRunningTest() in your controller itself and generate a sample response in your test class. Would look something like below:
In your test class

String response = '<your sample response>';
<classname>.testresult = response;


string testresult ;
Http http = new Http();       
        if (Test.isRunningTest()){
            result = testresult;
        } else {
// your code