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
Henry AkpalaHenry Akpala 

how to write a test class for Apex Rest Class

Hi All,

I am trying to write a test class for apex Rest class like the one below.  However I am having problem creating the test class because I don't see any documentation on how to create a variable for "RestRequest" object.  How do you create a values that can be passed to "RestRequest req"?

I tried creating a string of JSON message and passed it to the class but it didn't work.

 

//@HttpGet

global static String doGet (RestRequest req, RestResponse res){

 

 

//generate SOQL from request message dynamically 

String strQueryString =  req.requestURI.substring(req.requestURI.lastIndexOf('/')+1) + ': '

if (strQueryString.contains('doGet')){

strQueryString += '   '+ 'doGet1   ';

}

else if (strQueryString.contains('query')){

strQueryString += '   '+ 'query1   ';

}

for (String key : req.params.keySet()) {

strQueryString += '[ ] '+ key+ ' '+ req.params.get(key); 

}

 return strQueryString;

}

 

Any help will be greatly appreciated.

Regards

HenryAG

Best Answer chosen by Admin (Salesforce Developers) 
Henry AkpalaHenry Akpala

Found a solution,  In the test class these variables needs to be decleared

//@isTest

private class ApexRestSampleTest {

 

    static testMethod void myUnitTest() {

        RestRequest re = new RestRequest(); 

         RestResponse rp = new RestResponse();

         re.addHeader('httpMethod', 'GET');

         re.addParameter('Priority', 'High');        

        

         System.debug('STATUS:BEFORE Apex CLASS CALL');

         String result = ApexRestSample.doGet(re, rp);

         System.debug('STATUS:AFTER*** Apex CLASS CALL');

         System.assert(true);

   }

}

All Answers

Henry AkpalaHenry Akpala

Found a solution,  In the test class these variables needs to be decleared

//@isTest

private class ApexRestSampleTest {

 

    static testMethod void myUnitTest() {

        RestRequest re = new RestRequest(); 

         RestResponse rp = new RestResponse();

         re.addHeader('httpMethod', 'GET');

         re.addParameter('Priority', 'High');        

        

         System.debug('STATUS:BEFORE Apex CLASS CALL');

         String result = ApexRestSample.doGet(re, rp);

         System.debug('STATUS:AFTER*** Apex CLASS CALL');

         System.assert(true);

   }

}

This was selected as the best answer
venkateshpvenkateshp

I am trying to call apex rest resource from cliet with the following uri

 

https://[instance].salesforce.com/services/apexRest/[restResource]?username=[value]&password=[value]&id=[value]

 

how can i  access username, password , id values into the restresource.

i tried like below

String usrName = req.requestURI.substring(req.requestURI.IndexOf('?')+1);
System.debug('usrName'+usrName );
String passWord = req.requestURI.substring(req.requestURI.IndexOf('&')+1);
System.debug('passWord '+passWord );
String deviceId = req.requestURI.substring(req.requestURI.lastIndexOf('&')+1);
System.debug('deviceId '+deviceId);

 

but i am unable to get the values in the debug? what is the correct way to get the parameter values?

 

 

Thanks in advance,

venkatesh.