+ Start a Discussion
VamsiVamsi 

Test class with soql not returning any records

HI,
I have a class the returns the set of records based on createddate. So how to write a test class and test the results of that class ..
In a test class, If I instantiate a class and call the method then it covers me 100%. But when I try to verify the results it doesn't return me any records.

Also I have gone through some blog and got that we can provide values to created date on test class using test.setcreateddate(recordid,datetime)

Can someone please help me ...!!!
 
public with sharing class UnclaimCases
{
    public list<case> caseami {get;set;}
    public string strhours = Label.DateFilter; // custom label stores values in hours 
    public integer hours = integer.valueof(strhours);
    Datetime currenttime = system.now();
    datetime  acttime  = currenttime.addhours(-hours);
    public final static Id custid = Schema.SObjectType.case.getRecordTypeInfosByName().get('Support').getRecordTypeId();   

    public UnclaimCases()
    {

     caseami = [Select CaseNumber,Priority,Response_Due_Date__c,Response_time_elapsed__c from case where RecordTypeID =:custid AND (Status = 'Unclaimed' OR Status ='Escalated' ) AND Product_Type__c = 'A' AND Owner.Name = 'T Queue' AND Createddate>: acttime  AND Createddate< : currenttime  ORDER BY Priority  LIMIT 10000 ];
    }

    public pagereference inc()
     {
         Datetime currenttimeami = system.now();
         datetime  acttimeami  = currenttime.addhours(-hours);
         caseami = [Select CaseNumber,Priority,Response_Due_Date__c,Response_time_elapsed__c from case where RecordTypeID =:custid AND (Status = 'Unclaimed' OR Status ='Escalated' ) AND Product_Type__c = 'A' AND Owner.Name = 'T Queue' AND Createddate>: acttimeami  AND Createddate<: currenttimeami ORDER BY Priority LIMIT 10000 ];

         system.debug('caseami' + caseami );
         system.debug(' current datetime value ' + currenttimeami );
         system.debug(' act datetime value ' + acttimeami );
        return null;
    }


}
Test class 
@isTest
public class TestUnclaimCases 
{

    @isTest static void Unclamethod()
        {

            Id queueId =[select Id from Group where Name = 'T Queue' and Type = 'Queue'].Id;

            Id custid = Schema.SObjectType.case.getRecordTypeInfosByName().get('Support').getRecordTypeId(); 

            case c1 = new case(RecordTypeId = custid, Status = 'Unclaimed', Product_Type__c = 'A',Priority = 'SEV 1' , ownerid=queueId);
              insert c1;
    datetime delay1 = datetime.now.addhours(-2);
          Test.setcreateddate(c1,delay1);
            case c2 = new case(RecordTypeId = custid, Status = 'Rep On Site', Product_Type__c = 'A',Priority = 'SEV 2' , ownerid=queueId);
           insert c2;
  datetime delay2 = datetime.now.addhours(-2);
          Test.setcreateddate(c2,delay2);
            case c3 = new case(RecordTypeId = custid, Status = 'Escalated', Product_Type__c = 'A',Priority = 'SEV 2' );
            insert c3;
          datetime delay3 = datetime.now.addhours(-2);
          Test.setcreateddate(c3,delay3);

            UnclaimCases uc = new UnclaimCases();
            uc.inc();
            //list<case> fuc.caseami;
            system.assertEquals(2,uc.caseami.size());


        }
    }

 
Best Answer chosen by Vamsi
Prateek Singh SengarPrateek Singh Sengar
Hi Vamshi,
Since you have not set seealldata to true i believe you should create the queue as part of test data as well. 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Vamshi,
Since you have not set seealldata to true i believe you should create the queue as part of test data as well. 
This was selected as the best answer
VamsiVamsi
Thank you for the response Prateek.

But I am able to get the queue id in test class through query succesfully 
Prateek Singh SengarPrateek Singh Sengar
Hi Vamshi,
What is the value that you are receiving for currenttimeami  and acttimeami when you run the test class. Also after insert statement in test class can you try debugging the createdDate for the cases that you are creating to see if the created date is falling between the range.
 
VamsiVamsi
Thank you Prateek.

Yes you are right !!! we need to create a new queue in test class and also we need to change the owner.name in the main class (I got this value by from group object dynamically).