+ Start a Discussion
azu.shaik1.3965080575576013E12azu.shaik1.3965080575576013E12 

how to write test class to delete a record in apex rest api in salesforce

@HttpDelete
    global static void doDelete() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account account = [SELECT Id FROM Account WHERE Id = :accountId];
        delete account;
    }
AshwaniAshwani
Simply write a test class and call the method as:

// Create rest context before this method call
RestApiClassName.doDelete();

Rest context can be created in test class. Look as following link: http://blog.jeffdouglas.com/2012/03/21/writing-unit-tests-for-v24-apex-rest-services/

static testMethod void testDoDelete() {

    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/001rEtrq51wS4';  
    req.httpMethod = 'DELETE';


  }