+ Start a Discussion
JayChenJayChen 

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) {
       refreshMetrics(null);}

---
     
 HttpRequest req = new HttpRequest();
       String url = INSTANCE + '/services/data/v33.0/actions/standard/metricRefresh';
       req.setEndpoint(url);
       req.setMethod('POST');
       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);
       System.debug(res.getBody());
   }
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;

Class:

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