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
Roger PavelleRoger Pavelle 

Test case not calling database properly

Hi,

I am writing a test case against my custom controller.  I can get all the components to populate correctly, but the database query inside the function I am trying to test does not seem to be returning any results.  Any suggestions on what I need to change or add?

CONTROLLER function:
NOTE: I tested things and the buildWhereClause works correctly, so I'm not including that code as it is long.  Similarly, the sortOrder has a default value so it doesn't matter either.  The debugging messages show that neither query is returning any values (the record size returned by both is 0).

 public List<Bolete__c> getBoletes()
    {
        buildWhereClause();

        List<Bolete__c> resultsMax = Database.query(
            'SELECT Genus__c, Species__c, Common__c ' +
            'FROM Bolete__c ' +
            whereClause
        );
        //Total number of records
        recordSize = resultsMax.size();
       
        List<Bolete__c> results = Database.query(
            'SELECT id, Genus__c, Species__c, Common__c, imageURL__c, Edibility__c, Tells__c ' +
            'FROM Bolete__c ' +
            whereClause +
            'ORDER BY ' + sortOrder  +
            'LIMIT 20'
        );
        resultSize = results.size();
        System.debug('getBoletes  recordSize:' + recordSize + ' resultsize:' + resultSize);
        return results;
    }

TEST CASE: (NOTE: the Result Size should be 74, but is returning 0, so the assert fails.)
    @istest static void TestShowResultSize()
    {
        Test.startTest();
        PageReference pg = Page.BoleteSearch;
        pg.getParameters().put('boxNum','49');
        Test.setCurrentPage(pg);
        BoleteController ctrl = new BoleteController();
        ctrl.setChkBoxValue();
        List<Bolete__c> bols = ctrl.getBoletes();
        String wc = ctrl.currentWhereClause;
       
        Integer resultsize = ctrl.showResultSize;
        System.assert(resultsize==74, wc);
        Test.stopTest();
    }
 
David K.David K.
Just had this problem last week.  You need to amend your isTest header to ensure the test can query data

@isTest(SeeAllData=true)
Anirudh SinghAnirudh Singh
Hello Roger,

Please consider the below points:

1. Firstly, it is not a best practice to use SeeAllData=true, as it exposes all the Orgs data to your test class.

2. Dont worry about the records in your org. Test classes are written to test whether the code logic is correct or not.
And to solve your issue, you need to create data in the test class itself.

Use @TestSetup method to create data and then run your test method. The test method you are currently using will see all the records or data which you will create in the @TestSetup method.

Below is an example for the Test Class:

@isTest 
public class BoleteController_Test
{
    @TestSetup
    static void createTestRecords()
    { 
        List<Bolete__c> boleteRecordsList=new List<Bolete__c>();
        for(Integer i=0; i<100; i++)
        {
            Bolete__c newRecord=new Bolete__c();
            newRecord.Genus__c='test genus'+i;
            newRecord.Species__c='test species'+i;
            newRecord.Common__c='test common'+i;
            newRecord.imageURL__c='test imageURL'+i;
            newRecord.Edibility__c='test editibilty'+i;
            newRecord.Tells__c='test tells'+i;
            boleteRecordsList.add(newRecord);
        }
        
        insert boleteRecordsList;
    }

    static testMethod void validate()
    {
        Test.startTest();
        PageReference pg=Page.BoleteSearch;
        pg.getParameters().put('boxNum','49');
        Test.setCurrentPage(pg);
        
        BoleteController ctrl = new BoleteController();
        ctrl.setChkBoxValue();
        
        List<Bolete__c> bols = ctrl.getBoletes();
        String wc = ctrl.currentWhereClause;
       
        Integer resultsize = ctrl.showResultSize;
        System.assert(resultsize==74, wc);
        Test.stopTest();
    }
}

Please let me know if this helps.

Thanks and Regards,
Anirudh Singh