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
Ramana VRamana V 

Test Class to cover wrapper class

Hi All,
I am trying to cover wrapper class in calss. But it s not covering. I am sharing my class and test class.

Apex class
public class accountCController {
    @AuraEnabled (cacheable=true)
    public static List<accWrapper> getNameValues(List<string> acname){
        List<accWrapper> resultObject = new List<accWrapper>();
        string query = 'SELECT Id,Name FROM account  WHERE  Name';
        query+='=: acname';
        List<SObject> lstResult = Database.query(query);
        for (SObject res : lstResult) {
            accWrapper obj = new accWrapper();
            obj.testId = (String)res.get('Id');
            obj.testName = (String)res.get('Name');
            resultObject.add(obj);
        }       
        return resultObject;
    }
    public class accWrapper{
        @AuraEnabled
        public String testId;
        @AuraEnabled
        public String testName;
    }
	}

Test class:
@isTest
public class accountController_Test {
    static testMethod void testName(){  
        List<string> strList = new List<string>();
        strList.add('A');
        strList.add('B');
        accountCController.getNameValues(strList);
       
    }
}


Its covering "getNameValues" method but it is not covering wrapper class. Please help me on this.​​​​​​​

Thanks in advance

Regards,
​​​​​​​Ramana

Best Answer chosen by Ramana V
Abdul KhatriAbdul Khatri
Hi Ramana

In the test class you need to build your test data. Since you are SOQLing the Account by the name so the query is returning 0 and not going into the For loop. Therefore wrapper class is not covering. Also it is a best practise to put the asset in the end, make your test legit.

Change your test class as follwoing
@isTest
public class accountController_Test 
{
    static testMethod void testName()
    {
        List<Account> accountToInsert = new List<Account>{
            new Account (Name = 'A'),
            new Account (Name = 'B')    
        }; 
        insert accountToInsert;    
        
        List<string> strList = new List<string>();
        strList.add('A');
        strList.add('B');
        List<accountCController.accWrapper> wrapperList = accountCController.getNameValues(strList);
        
        system.assert(wrapperList.size() == 2);
    }
}

Please mark it the best if resolved
 

All Answers

MichelCRMichelCR
Hi Ramana,

Try reading the properties testId and testName from the result obtained from calling getNameValues(). Something like adding the following lines to test:
....
List<accWrapper> result = accountCController.getNameValues(strList);
String testId, testName;
for(accWrapper r : result) {
    testId = r.testId;
    testName = r.testName;
}

You could add some assert statements from those results.

I hope this helps
Abdul KhatriAbdul Khatri
Hi Ramana

In the test class you need to build your test data. Since you are SOQLing the Account by the name so the query is returning 0 and not going into the For loop. Therefore wrapper class is not covering. Also it is a best practise to put the asset in the end, make your test legit.

Change your test class as follwoing
@isTest
public class accountController_Test 
{
    static testMethod void testName()
    {
        List<Account> accountToInsert = new List<Account>{
            new Account (Name = 'A'),
            new Account (Name = 'B')    
        }; 
        insert accountToInsert;    
        
        List<string> strList = new List<string>();
        strList.add('A');
        strList.add('B');
        List<accountCController.accWrapper> wrapperList = accountCController.getNameValues(strList);
        
        system.assert(wrapperList.size() == 2);
    }
}

Please mark it the best if resolved
 
This was selected as the best answer