You need to sign in to do that
Don't have an account?
inbox outbox 7
I am trying to get full code coverage. Can anyone give their input on the below code?
========================== CLASS==================== @httpDelete global static String deleteAccount(){ RestRequest req= Restcontext.request; String requestURI= req.requestURI ; String accountId= requestURI.substringAfterLast('/'); List<Account> accList= [SELECT ID, Name, Rating, Description, Industry, Phone, Fax FROM Account WHERE ID =: accountId]; if(accList != null && accList.size() >0 ){ try{ DELETE accList; return '{"message": "Account deleted"}'; }Catch(system.Exception ex){ String errorMessage= ex.getMessage(); return '{"message":"'+errorMessage+'"}'; } }else{ return '{"message": "No record found"}'; } } ====================TEST CLASS=================== @isTest public static void deleteAccountTest(){ String recordId= createTestRecord(); String url= '/services/apexrest/v1/Account'+recordId; RestRequest req= New RestRequest(); req.httpMethod= 'DELETE'; req.requestURI= url; RestContext.request= req; Test.startTest(); AccountManager.deleteAccount( ); Test.stopTest(); } static Id createTestRecord(){ Account testAcc= New Account(); testAcc.Name= 'testAccount'; INSERT testAcc; Contact testCon= New Contact(); testCon.LastName= 'test contact'; testCon.AccountId= testAcc.Id; INSERT testCon; return testAcc.Id; }I have trouble with the try catch block.
Can you please check the below reference article.
https://salesforce.stackexchange.com/questions/16727/test-code-coverage-catch-block
If this helps, please mark it as best answer.
Regards,
Ankaiah Bandi
I updated the bold part of the code with the Test.isRunningTest().
TEst class is the same. I did not make any changes.