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 have a problem with the code coverage. Please help me with the test class to get full code coverage.

Bold part of the code is not covered in the test class. 
@httpPatch
    global static updateWrapper updateAccount(String Desciption){
        RestRequest req= Restcontext.request;
        String requestURI= req.requestURI;
        String accountId= requestURI.substringAfterLast('/');
        
        List<Account> accList= [SELECT ID, Description FROM Account WHERE ID =: accountId];
        Account acc;
        updateWrapper updateWrapper= new updateWrapper();
        IF(accList != null && accList.size() >0){
            <b>acc= new Account(Id=accountId, Description= Desciption);
            UPDATE acc;
            updateWrapper.accRec= acc;
            updateWrapper.message= 'Account updated';</b>
        }else{
            updateWrapper.message= 'Account record not found';
        }
        return updateWrapper;    
    
 global class updateWrapper{
        global Account accRec;
        global String message;
    }
Test class:
@isTest
    public static void updateAccountTest_else(){
        String recordId= createTestRecord();
        String url= '/services/apexrest/v1/Account'+recordId+'1';
            
        RestRequest req= New RestRequest();
        req.httpMethod= 'PATCH';
        req.requestURI= url;
        
        RestContext.request= req;
       
        Test.startTest();
        AccountManager.updateAccount('some random text with else');
        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;
        
    }


 
PriyaPriya (Salesforce Developers) 
Hi 

Its seems like you are not creating the Account record at right place. 

Please try with this code and let see if it cover the code or not.
@isTest
    public static void updateAccountTest_else(){
        String recordId= createTestRecord();
        String url= '/services/apexrest/v1/Account'+recordId+'1';
            
        RestRequest req= New RestRequest();
        req.httpMethod= 'PATCH';
        req.requestURI= url;
        
        RestContext.request= req;
       
        Test.startTest();
 
 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;
        AccountManager.updateAccount('some random text with else');

        Test.stopTest();
    }


Regards,

Priya Ranjan