+ Start a Discussion
inbox outbox 7inbox outbox 7 

Can anyone help me with the test class? I could not cover the wrapper class variables in the test class

Below is the code:
Test class doesn't cover the bold part of the code. 
@httpGet
    global static accountWrapper accountInformation(){
        RestRequest req= Restcontext.request;
      // String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        String requestURI= req.requestURI;
        String accountId= requestURI.substringAfterLast('/');
        List<Account> accList= [SELECT ID, Name, Rating, Description, Industry, Phone, Fax
                                FROM Account WHERE ID =: accountId];
        
        List<Contact> conList= [SELECT ID, Name, FirstName, LastName, Email, Phone 
                                FROM Contact WHERE accountId IN: accList ];
        
        List<Case> caseList= [SELECT Id, CaseNumber, Subject, Description, Status, Owner.Name
                              FROM Case WHERE accountId IN: accList];
        
        accountWrapper wrapper= new accountWrapper();
        if(!accList.isEmpty()){
            wrapper.accountRecord= accList.get(0);
            wrapper.conList= conList;
            wrapper.caseList= caseList;
        }
        
        return wrapper;
    }

  global class accountWrapper{
        global Account accountRecord;
        global List<Contact> conList;
        global List<Case> caseList;
    }
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;
        
        try{
        Test.startTest();
        AccountManager.deleteAccount(	);
        Test.stopTest();
        }catch(system.Exception ex){
            system.assertNotEquals(null, ex.getMessage());
        }
    }


    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;
        
    }



 
Best Answer chosen by inbox outbox 7
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!

All Answers

CharuDuttCharuDutt
Hii Inbox
Try Below Class
@isTest
public class wdadaw {
@isTest
    public static void deleteAccountTest(){
         Account testAcc= New Account();
        testAcc.Name= 'testAccount';
        INSERT testAcc;
        
        Contact testCon= New Contact();
        testCon.LastName= 'test contact';
        testCon.AccountId= testAcc.Id;
        INSERT testCon;
        String url= '/services/apexrest/v1/Account/'+testAcc.Id;
            
        RestRequest req= New RestRequest();
        req.httpMethod= 'DELETE';
        req.requestURI= url;
        
        RestContext.request= req;
        
        try{
        Test.startTest();
        ClassName.accountInformation();
        Test.stopTest();
        }catch(system.Exception ex){
            system.assertNotEquals(null, ex.getMessage());
        }
    }
}
Please Mark It As Best Answer If it Helps
Thank You!
CharuDuttCharuDutt
Hii Inbox
Please Close your Query By Marking It As Best  Answer So It Also Helps Other In Future
inbox outbox 7inbox outbox 7
@CharuDutt.

My apoligies, I put the wrong test class (httpDelete). Thank you so much for replying. 
I put the correct code below for the httpGet. 
 
@isTest
    public static void getAccountTest()
    {
        String recordId= createTestRecord();
        // Not sure why we are not using the base url, can be any url doesn't have to be a proper url and so we did not use base url.
        
        String url= '/services/apexrest/v1/Account'+recordId;
        RestRequest req= new RestRequest();
        req.requestURI= url;
        req.httpMethod= 'GET';
        
        RestContext.request= req;
        Test.startTest();
        AccountManager.accountWrapper wrap= AccountManager.accountInformation();
        Test.stopTest();
        system.assertNotEquals(null, wrap);
    } 

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;    
    }

Class which is not completely covered. Again, my apologies.
Bold part is not covered. 
@httpGet
    global static accountWrapper accountInformation(){
        RestRequest req= Restcontext.request;
      // String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        String requestURI= req.requestURI;
        String accountId= requestURI.substringAfterLast('/');
        List<Account> accList= [SELECT ID, Name, Rating, Description, Industry, Phone, Fax
                                FROM Account WHERE ID =: accountId];
        
        List<Contact> conList= [SELECT ID, Name, FirstName, LastName, Email, Phone 
                                FROM Contact WHERE accountId IN: accList ];
        
        List<Case> caseList= [SELECT Id, CaseNumber, Subject, Description, Status, Owner.Name
                              FROM Case WHERE accountId IN: accList];
        
        accountWrapper wrapper= new accountWrapper();
        if(!accList.isEmpty()){
            wrapper.accountRecord= accList.get(0);
            wrapper.conList= conList;
            wrapper.caseList= caseList;
        }
        
        return wrapper;
    }

  global class accountWrapper{
        global Account accountRecord;
        global List<Contact> conList;
        global List<Case> caseList;
    }



 
CharuDuttCharuDutt
Hii Inbox
@isTest
public class wdadaw {
@isTest
    public static void deleteAccountTest(){
         Account testAcc= New Account();
        testAcc.Name= 'testAccount';
        INSERT testAcc;
        
        Contact testCon= New Contact();
        testCon.LastName= 'test contact';
        testCon.AccountId= testAcc.Id;
        INSERT testCon;
        String url= '/services/apexrest/v1/Account/'+testAcc.Id;
            
        RestRequest req= New RestRequest();
        req.httpMethod= 'DELETE';
        req.requestURI= url;
        
        RestContext.request= req;
        
        try{
        Test.startTest();
        ClassName.accountInformation();
        Test.stopTest();
        }catch(system.Exception ex){
            system.assertNotEquals(null, ex.getMessage());
        }
    }
}
Please Mark It As Best Answer If it Helps
Thank You!
inbox outbox 7inbox outbox 7
@CharuDutt. 
It is httpGet method not delete, also code coverage did not change. My reason for putting this questions is to get full code coverage. My test class works fine, it is just that I wanted to get the full code coverage but the code you have mentioned did not increase the code coverage. 

Again my apoligies for not putting the correct code in the beginning. 
I would like to get more input on this. 
Thank you @CharuDutt. 
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!
This was selected as the best answer