0
public static String httpJsonResponseString;
 HTTPResponse res_usage;
    if(!Test.isRunningTest()){
        res_usage = http_usage.send(req);
    }else{
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(httpJsonResponseString);
        res.setStatusCode(200);
        res_usage = res;
    }   

in this i am using condition is res.setbody(variable) this is running from test class, now how i satisfy this from test class?? i just want that json string is pass in this and get the value, how i can satisfy that because when i tried to do it with simple inserting the account then it is getting null pointer exception. Thanks!!

3

Test a Callout with HttpCalloutMock trailhead module is quite good in explaining on how to write proper test.

Also refer Testing HTTP Callouts by Implementing the HttpCalloutMock Interface.

  • But this is not a Mock callout..actually in this we are passing a hardcoded id in json string 'account_name":"20|20 Research","sf_account_id":"001d000001nBAwtAAG" like this, so just save from harcoded string we are using variable, so when i pass the value in this from test class then it get the value in this variable. – sonam Jan 22 '18 at 10:53
  • and this is i don't know how to pass the value in variable from test class? – sonam Jan 22 '18 at 10:53
  • With HttpCalloutMock, you don't need to skip the callout with Test.isRunningTest(). Its testing mock test in right way. – Raul Jan 22 '18 at 10:58
  • yes may be you are right but this is not written by me, it is written by someone other and i need to create only test class of this, so i can't do any changes in main code...so can you please tell me the solution for this which i have posted? – sonam Jan 22 '18 at 11:00
  • Have you had a look at the documentation attached? it clearly explains on how to write a test class for HTTP request callout. I didn't wanted to quote the whole documentation in the answer. Have already pointed you in right direction, I think. – Raul Jan 22 '18 at 11:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.