function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
inbox outbox 7inbox 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. 
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

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
inbox outbox 7inbox outbox 7
I tried using test.isRunningtest. But I keep getting error with the return statement, it must be a string 
I updated the bold part of the code with the Test.isRunningTest().
 
@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{
                if(!Test.isRunningTest()){
                    DELETE accList; 
                    return '{"message": "Account deleted"}';
                }
            }Catch(system.Exception ex){
                if(Test.isRunningTest()){
                    String errorMessage= ex.getMessage();
                    return '{"message":"'+errorMessage+'"}';
                }else{
                    return '{"message": "No record found"}';
                }
            }
        }else{
            return '{"message": "No record found"}';
        }
    }

TEst class is the same. I did not make any changes. 
@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;
        
    }