+ Start a Discussion

how to write the test class for delete method in apex rest api in salesforce

i am able to write test the class for the HttpPost and HttpPAtch but i was stuck to write the test method for the delete method . Please help me how write test class for the below delet method.

global static string deleteAccountById() {

    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    // phpId related to Product object
    string phpId = RestContext.request.params.get('phpId');
    // phpId related to Attributes
    List<Account> acc =[ select id from Account where Php_id__c =: phpId];       

   if(acc.size() > 0){

    delete acc;

    return ' sucessfully delete the account';

           return 'The requested resource not found in system to delete ' + '=' +phpId ;


And my test method


Private class AccountCreationTest{

static testMethod void deleteAccountById(){

    Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
    string rtId = AccountRecordTypeInfo.get('Affiliate').getRecordTypeId();
      List<Account> acc1= new List<Account>();
        Account acc= new Account(recordtypeid = rtId );
        acc.Name = 'TestName1';
        acc.First_Name__c = 'Test firstname';
        acc.Last_Name__c = 'Test lastname';
        acc.Email__c = 'Testmail@gmail.com';
        acc.Php_id__c ='1234';
        acc.BillingPostalCode= '600119';
        acc.BillingStreet= 'test street';
        acc.BillingCity= 'Nellore';
        acc.BillingState= 'Andhra Pradesh';
        acc.BillingCountry= 'India';
        insert acc1;
            delete acc1;
    String JSONMsg = JSON.serialize(acc);
    RestRequest req = new RestRequest();
    req.requestURI = 'https://sandbox-togethersave.cs5.force.com/services/apexrest/restAccount?phpId=123';
    req.httpMethod = 'Delete';
    req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
    RestResponse res = new RestResponse();
    RestContext.request = req;
    RestContext.response = res;