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
balaji raobalaji rao 

integration test class with rest api

how to cover an end point and json string in test class of rest api controller ?
Raj VakatiRaj Vakati
Refer this links

https://www.thinkaholics.com/salesforce/a-simple-apex-test-class-for-rest-api/

https://blog.jeffdouglas.com/2012/03/21/writing-unit-tests-for-v24-apex-rest-services/

http://www.sfdcstuff.com/2017/08/creating-apex-rest-services-and-testing.html
 
@isTest
private class Test_MemberRestSvc {
  
  static {
    // setup test data  
  }
  
  static testMethod void testDoGet() {
        
    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();

    // pass the req and resp objects to the method     
    req.requestURI = 'https://cs9.salesforce.com/services/apexrest/v.9/member/me/results/today';  
    req.httpMethod = 'GET';

    MemberRestSvc.ReturnClass results = MemberRestSvc.doGet(req,res);
    
    System.assertEquals('true', results.success);
    System.assertEquals(10, results.records.size());
    System.assertEquals('Query executed successfully.', results.message);
 
  }
  
}

 
{tushar-sharma}{tushar-sharma}
You need to pass that as parameters:
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();

//Test POST API
req.requestURI = '/services/apexrest/Account';  //Request URL
req.httpMethod = 'POST';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;
String recId = MyRestResource.dopost('Test','9887988798','https://newstechnologystuff.com');

Check the link for complete detailed explanation: https://newstechnologystuff.com/2020/05/27/test-salesforce-rest-api-using-postman/
Suraj Tripathi 47Suraj Tripathi 47
Hi Balaji,
You can write test class two ways-
  1)By using static resources-In this you need to go 
   Developer Console-> select File -> New -> Static Resource.
  2)By implementing Interface.
   Create class->then implement interface.
Go through this link for more information.
https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts#apex_integration_rest_callouts_resources

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi