+ 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



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);

You need to pass that as parameters:
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();

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.

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

Thank you!

Suraj Tripathi