• Amruta Gujarathi 5
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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