You need to sign in to do that
Don't have an account?
Create Test Case for WebService
Hi,
I have an Apex class which is shown as below.
global class HttpRESTConnection{
WebService static String postMSG(String body, String url){
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(body);
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
}
when I include this class in the package and upload the package, I saw an error message stating I do not have test method for this class.
Can someone help me to create a simple test case for this class?
Thanks
Read this :
http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
There's a specific section in there on how to handle test cases for callouts.
I create a simple test case as following, but I got error message stating "HttpRESTConnection class does not have a valid test case" even though I add "HttpRESTConnectionTestClass" in the package.
@isTest
private class HttpRESTConnectionTestClass {
static testMethod void validateHttpRESTConnection() {
String body;
HttpRESTConnection oConn = new HttpRESTConnection();
}
}
Can you please give me some suggestion?
Thanks
global class HttpRESTConnection{
WebService static String postMSG(String body, String url){
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(body);
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
static testMethod void validateHttpRESTConnection() {
String body;
HttpRESTConnection oConn = new HttpRESTConnection();
body = postMSG('Message','https://www.idhsie.com');
}
}
I modified my web service class as above, but I still got "
I use Scontrol javascript page to call this function, so I have to use WebService type.
Can someone please point out what I should do? Thanks.
You'll need to make your test method "public" for it to be seen.
Cheers,
Ron