how to write test code for below rest api service

global with sharing class Create_HH_Survey

global static void doDelete() 
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    String MobileNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    List<Test_Survey__c> result = [SELECT Id,Test_Mobile_1__c FROM Test_Survey__c WHERE Test_Mobile_1__c= :MobileNumber];
      delete result;
      String returnres ='Record Deleted Sucessfully';
      res.responseBody = Blob.valueOf(returnres); 
      String Error='Record Not Found';
      res.responseBody = Blob.valueOf(Error); 

}//end of deleteSurveyById

Test Class

static testMethod void testDeleteMethod()
        Test_Survey__c acc = new Test_Survey__c();
        acc.Test_Consent_Status__c='Consent For Interview';

        insert acc;

        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/CreateHH_Survey/acc.id';
        request.httpMethod = 'DELETE';
        RestContext.request = request;
        String record =Create_HH_Survey.deleteSurveyById();

        List<Test_Survey__c> ListAcct = [SELECT Id FROM Test_Survey__c WHERE Id=:acc.id];
        **System.assert(ListAcct.size() ==0 );**            
  • You seem to have written a test class already. What is your specific question about this code? If you're encountering an error, please edit the verbatim text into your question and mark the line number. – David Reed Feb 6 at 12:20
  • System.QueryException: List has no rows for assignment to SObject getting this Error – user63974 Feb 6 at 13:30
  • Please Help me to Write correct Test method for Rest API delete Method – user63974 Feb 6 at 13:50

You are hard-coding a Salesforce Id in the URL parameter for your REST class:

req.requestURI = '/services/apexrest/CreateHH_Survey/a2V5D00000016A3z';  //Request URL
req.httpMethod = 'Delete';        //HTTP Request Type
req.requestBody = Blob.valueof(JsonMsg);
RestContext.request = req;
RestContext.response= res;

That Salesforce Id does not exist or is not visible in test context. You need to use the Id of the Test_Survey__c record you created in test context, acc.

Your test class has some other incongruities, including variables you never use and a JSON body your REST class doesn't look at.

Your actual QueryException comes here, though:

Test_Survey__c  deletedsurvey = [SELECT ID,IsDeleted FROM Test_Survey__c WHERE ID = :abc  ALL ROWS];

Your class doesn't return a Salesforce Id; it returns a string. Hence, your query returns no rows. When you assign an empty query result to a single sObject, you will get a QueryException. You must query against the actual Salesforce Id, created in test context, that you passed to your REST class.

Your Updates

You've updated your REST URL incorrectly.

    request.requestUri ='/services/apexrest/CreateHH_Survey/acc.id';
    request.httpMethod = 'DELETE';

You are now passing the literal string acc.id. You need to concatenate (+) the Id to the URL for your class to read it.

You should probably revert your change to query based on Mobile Number, since you are not passing the Mobile Number.

  • I have Modified Test class with new Name testDeleteMethod But my System.assert(ListAcct.size() ==0 ); Fail if i delete a record than ListAcct Should be Null or Empty – user63974 Feb 7 at 6:53
  • Look carefully at how you are constructing that URL. Your class is but getting the data it expects. You're now hard coding a value that is not a Salesforce Id. – David Reed Feb 7 at 12:38
  • i replaced the ID & delete by using mobile no – user63974 Feb 8 at 7:16
  • List<Test_Survey__c> ListSurvey = [SELECT Id FROM Test_Survey__c WHERE Id=:acc.id]; – user63974 Feb 8 at 7:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.