+ Start a Discussion
Satish PalyamSatish Palyam 

Select Count does not return results in Test class

Hi Experts, 

I've created a test class like here. 

class TestdeleteOldProcessedUserChangeEntries {
    // CRON expression: midnight on March 15.
    // Because this is a test, job executes
    // immediately after Test.stopTest().
    public static String CRON_EXP = '00 32 17 05 3 ? 2015'; //'0 0 0 15 3 ? 2022';
    static testmethod void test() {
//     Assuming that there are no userchange entries put into the table during testing.. get the table count to compare before and after
  Integer initcount1 = [SELECT COUNT() FROM user_change__c];
system.debug('initcount1' + initcount1);
        // Schedule the test job
        String jobId = System.schedule('deleteOldProcessedUserChangeEntries',
                                       new deleteOldProcessedUserChangeEntries());
        // Get the information from the CronTrigger API object
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
                          FROM CronTrigger WHERE id = :jobId];
        // Verify the expressions are the same
        // Verify the job has not run
        System.assertEquals(0, ct.TimesTriggered);
        // Verify the next time the job will run
        System.assertEquals('2022-03-15 00:00:00',
        // Verify the scheduled job hasn't run yet.
  Integer initcount2 = [SELECT COUNT() FROM user_change__c];
        system.debug('initcount2' +initcount2);
         System.assertEquals(initcount1, initcount2);
        // Now that the scheduled job has executed after Test.stopTest(),
        // fetch the new merchandise that got added.
  Integer initcount3 = [SELECT COUNT() FROM user_change__c];
        system.debug('initcount3' + initcount3);  
         System.assertEquals(initcount2-initcount3, 1);

Select Count is not returning me any count...? any ideas. ?

Please advise. 
You have to insert your test data (best case) or you can use @isTest(SeeAllData=true)


Forewarning on @isTest(SeeAllData=true):

If your production org has lots of records, things can take a LONG time to finish tests; we went from a 30-minute deploy to a 5-minute deploy once we got rid of all of those SeeAllData annotations.  It's best practice to have your test methods create test data.  That way you always know what you're testing against and what to expect.