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
iatschuckiatschuck 

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

David VPDavid VP

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.

iatschuckiatschuck

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

iatschuckiatschuck

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 "

Apex ClassHttpRESTConnectionNo testMethods found in the selected Apex code for the package
"

 

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.

 

 

Ron WildRon Wild

You'll need to make your test method "public" for it to be seen.

 

Cheers,

Ron