You need to sign in to do that
Don't have an account?
azu.shaik1.3965080575576013E12
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.
@HttpDelete
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';
}
else{
return 'The requested resource not found in system to delete ' + '=' +phpId ;
}
}
And my test method
@IsTest
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';
acc1.add(acc);
insert acc1;
if(acc1.size()>0){
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;
AccountCreation.deleteAccountById();
}
}
@HttpDelete
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';
}
else{
return 'The requested resource not found in system to delete ' + '=' +phpId ;
}
}
And my test method
@IsTest
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';
acc1.add(acc);
insert acc1;
if(acc1.size()>0){
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;
AccountCreation.deleteAccountById();
}
}