+ Start a Discussion
Lek LextechLek Lextech 

Test class is cover for loop

Hi All, my class and test class below is just cover for 60%, I found that is not cover on for loop. How can I modify test class to cover for loop?
Many Thanks.

customLookUpController:
public class customLookUpController {
    @AuraEnabled
    public static List < sObject > fetchLookUpValues(List<String> fieldstoShow, String searchKeyWord, String ObjectName, String whereCondition, String limitRecord) {
        system.debug('ObjectName-->' + ObjectName);
        String searchKey = searchKeyWord + '%';
		String moreFields = ','+String.Join(fieldstoShow,',');
            
        List < sObject > returnList = new List < sObject > ();
      
        // Create a Dynamic SOQL Query For Fetch Record List with some LIMIT   
        String sQuery =  'select id, Name'+moreFields+' from '+ObjectName + ' where Name LIKE: searchKey '+whereCondition +' order by createdDate DESC limit '+limitRecord;
        List < sObject > lstOfRecords = Database.query(sQuery);
        
        for (sObject obj: lstOfRecords) {
            returnList.add(obj);
        }
        return returnList;
    }
}

customLookUpTestController:
@isTest(SeeAllData=true)
private class customLookUpTestController {
	@isTest static void testCustomLookup(){
		List <String> fieldstoShow = new List <String>();
        fieldstoShow.add('Id');
        fieldstoShow.add('Name');
        
        List < sObject > returnList = customLookUpController.fetchLookUpValues(fieldstoShow, '', 'Account', '', '5');
        System.assert(returnList != null);
        
    }
}

User-added image
Best Answer chosen by Lek Lextech
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
@isTest()
private class customLookUpTestController 
{
	@isTest static void testCustomLookup()
	{
		Account acc = new Account();
			acc.name ='Test';
		insert acc;
		
		List <String> fieldstoShow = new List <String>();
        fieldstoShow.add('Id');
        fieldstoShow.add('Name');
        
        List < sObject > returnList = customLookUpController.fetchLookUpValues(fieldstoShow, 'Test', 'Account', '', '5');
        System.assert(returnList != null);
        
    }
}

Let us knw if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
@isTest()
private class customLookUpTestController 
{
	@isTest static void testCustomLookup()
	{
		Account acc = new Account();
			acc.name ='Test';
		insert acc;
		
		List <String> fieldstoShow = new List <String>();
        fieldstoShow.add('Id');
        fieldstoShow.add('Name');
        
        List < sObject > returnList = customLookUpController.fetchLookUpValues(fieldstoShow, 'Test', 'Account', '', '5');
        System.assert(returnList != null);
        
    }
}

Let us knw if this will help you
 
This was selected as the best answer
Lek LextechLek Lextech
Thanks, Amit. ^_^
 
Amruta Gujarathi 5Amruta Gujarathi 5
Can you please let me know how Can I modify test class to cover following code?
 
public with sharing class avMultiSelectLookUpController {
	@AuraEnabled
    public static List<sObject> fetchLookUpValues(String searchKeyWord, String objectName, List<sObject> excludeItemsList) {
        String searchKey = '%' + searchKeyWord + '%';
        List<sObject> returnList = new List<sObject>();
 
        List<string> lstExcludeitems = new List<string>();
        for(sObject item : excludeItemsList) {
            lstExcludeitems.add(item.Id);
        }
        
        // Dynamic SOQL query for fetch record list and exclude already selected records  
        String sQuery =  'SELECT Id, Name FROM ' +objectName + ' WHERE Name LIKE: searchKey AND Id NOT IN : lstExcludeitems ORDER BY createdDate DESC limit 10';
        List <sObject> lstOfRecords = Database.query(sQuery);
        
        for (sObject obj: lstOfRecords) {
            returnList.add(obj);
        }
        return returnList;
    }
}
Thanks in advance! :)