+ Start a Discussion
salesforce123salesforce123 

how to write callout web services testclass programs in salesforce

Hi i deveop the webservices class program.but i want to develop the test class program.
how to do this....u have any reference code pls share with me.  Thanks
Grazitti TeamGrazitti Team
Hi,

Test class cannot be used to test Web service callout. The Test.isRunningTest() method is a useful method to bypass web service callouts when running automated test cases that would otherwise fail.

For example:

public String sendHttpRequest() {
  ...
  HttpRequest req = new HttpRequest();
  req.setEndpoint(endpointUrl);
  req.setMethod(method);
  req.setBody(bodyStr);

  Http http = new Http();
  if(!Test.isRunningTest()) {
    HttpResponse res = http.send(req);
    return res.getBody()
  } else {
    // You can prepare some simulated data for your test class here
    return simulatedData;

  }
}

Thanks,
Grazitti Team.
Deepak Kumar ShyoranDeepak Kumar Shyoran
For that you have to create mock Test Class

Please visit https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm it describe how to create test class for a class having web-service.

Please mark my answer as a best solution to your question to help others if it solves your problem.