0

I have an seemingly simple aura enabled class for agreement lookup which returns maximum 8 records using dynamic SOQL. This class works fine from UI perspective. No issues so far while testing. My only problem is getting the test class to pass by returning 8 rows from apex controller class. While running test class, Assertion fails by returning 0 rows - System.AssertException: Assertion Failed: Expected: 8, Actual: 0

Not sure what am I missing here. Please suggest.


    @AuraEnabled
    public static List<sObject> fetchLookUpValues(String searchKeyWord, String extraQueryCondition, String ObjectName, String fieldNames) {
    
        String searchKey = '%' + searchKeyWord + '%';
        List<sObject> returnList = new List <sObject> ();
        String sQuery = ' ';
        
        sQuery =  'SELECT Id, Name, RecordType.Name FROM ' + ObjectName + 
           ' WHERE (Name LIKE :searchKey ' + extraQueryCondition + ') ORDER BY Name ASC LIMIT 8';
        
        List<sObject> listOfRecords = Database.query(sQuery);
        system.debug('listOfRecords =' + listOfRecords);    // returns 0 
        
        for (sObject obj: listOfRecords) {
            returnList.add(obj);
        }
        
        return returnList;
    }

}

Following is corresponding test class -


@isTest(SeeAllData=false)
public class CustomLookupController_Test {

    
    @testSetup 
     public static void create10Agreements() {
        List<Agreement__c> testAgreements = new List<Agreement__c>();
        for(Integer i = 1; i <= 10; i++){
            Agreement__c a = APTS_TestDataFactory.create_NDA_Agreement_For_Plan_Agreement_Data();
            a.Name = 'test Agreement '+ i;
            testAgreements.add(a);
         }  
        insert testAgreements;
    }


    public static TestMethod void testAgreementFetchLookUpValues10(){

        Test.startTest();
        create10Agreements();  // works fine till this point 

        List<Agreement__c> agreements1 = [SELECT Id, Name, RecordType.Name from Agreement__c limit 10 ];
        system.debug('Size of agreements1 :' + agreements1.size());  // returns 10 
        
        List<Agreement__c> agreements = CustomLookupController.fetchLookUpValues('test Agreement', '', 'Agreement__c', 'Name');
        system.debug('Size of agreements :' + agreements.size());
        Test.stopTest();
       
        // Expecting 8 since there is a limit of 8 returned from the query.
        System.assertEquals(8, agreements.size());   //System.AssertException: Assertion Failed: Expected: 8, Actual: 0
        
    }    
    
}
4
  • without seeing CustomLookupController.fetchLookupValues it will be hard to know the answer here. Your test data probably doesn't match what the controller expects
    – cropredy
    Dec 17 '20 at 18:09
  • Yes i have already put the method CustomLookupController.fetchLookupValues . I missed to paste the 1st line - public with sharing class CustomLookupController {
    – kumar
    Dec 17 '20 at 18:46
  • N.B. the @testSetup method runs automatically when the testmethod starts so no need to explicitly invoked create10Agreements
    – cropredy
    Dec 17 '20 at 19:08
  • You are right, please discard that. i added that later while pasting here.
    – kumar
    Dec 17 '20 at 19:21
0

Just wanted to update with the solution. There was a trigger which was causing to change the agreement Name to some other value. Because of this, the select query was not able to search with the given name- 'test Agreement'. Glad we found the root cause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.