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
Jos VervoornJos Vervoorn 

Test class is not touching REST API

Hi,

I have created a simple Rest API endpoint however my test class is not touching the endpoint at all. The API URL mapping is

//** services/apexrest/accounts/0016E00000YNC49/entitlements
@RestResource(urlMapping='/accounts/*/entitlements')
Global with sharing class U4_REST_AccountEntitlements {
    @HttpGet
    global static Account getAccount(){

and returns entitlement values. Using REST Explorer within workbench it works perfectly but when I run the test class it is not returning any values. In fact ... the result is [REST_AccountEntitlements_TEST].RestResource response:RestResponse:[headers={}, responseBody=null, statusCode=null]​

Any help would be much appreciated.
Test.startTest();

           Id recordId = NewServiceAccount.Id;
           RestRequest request = new RestRequest();
           RestResponse response = new RestResponse();
           request.httpMethod = 'GET';

           Request.requestURI  ='/services/apexrest/accounts/'+NewServiceAccount.Id+'/entitlements';
           System.debug(LoggingLevel.Info,'+*+*+*+*+*+*+* '+NewServiceAccount.id);

           System.debug(LoggingLevel.Info,'[REST_AccountEntitlements_TEST].RestResource response:'+Request.requestURI);

           RestContext.request = request;
           RestContext.response = response;

           System.debug(LoggingLevel.Info,'[REST_AccountEntitlements_TEST].RestResource response:'+response); 
  
        Test.stopTest();

 
Best Answer chosen by Jos Vervoorn
v varaprasadv varaprasad
Hi Jos,

Try This : 
 
Test.startTest();
			//create NewServiceAccount record and pass it to URI.
           
		   Id recordId = NewServiceAccount.Id;
           RestRequest request = new RestRequest();
           RestResponse response = new RestResponse();
           request.httpMethod = 'GET';

           Request.requestURI  ='/services/apexrest/accounts/'+NewServiceAccount.Id+'/entitlements';
           
		   Request.httpMethod = 'GET';

           RestContext.request = request;
           RestContext.response = response;

           
		  
		  //Call class here 
          account acc = U4_REST_AccountEntitlements.getAccount(); 
		  system.debug('==acc=='+acc);
        Test.stopTest();

More info :

https://salesforce.stackexchange.com/questions/190707/test-class-for-rest-api-get


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 

All Answers

v varaprasadv varaprasad
Hi Jos,

Try This : 
 
Test.startTest();
			//create NewServiceAccount record and pass it to URI.
           
		   Id recordId = NewServiceAccount.Id;
           RestRequest request = new RestRequest();
           RestResponse response = new RestResponse();
           request.httpMethod = 'GET';

           Request.requestURI  ='/services/apexrest/accounts/'+NewServiceAccount.Id+'/entitlements';
           
		   Request.httpMethod = 'GET';

           RestContext.request = request;
           RestContext.response = response;

           
		  
		  //Call class here 
          account acc = U4_REST_AccountEntitlements.getAccount(); 
		  system.debug('==acc=='+acc);
        Test.stopTest();

More info :

https://salesforce.stackexchange.com/questions/190707/test-class-for-rest-api-get


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
This was selected as the best answer
Jos VervoornJos Vervoorn
How could I overlook? Thanks works like a charm. .. 100%