0

I have a class the returns the set of records based on lastmodified date. 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

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 LastModifiedDate >: acttime  AND LastModifiedDate < : 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 LastModifiedDate >: acttimeami  AND LastModifiedDate <: 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());


        }
    }
2

1 Answer 1

0

You are getting 100% coverage because query is getting successfully executed inside your constructor.

But it is not returning any record as it does not find the data matching with the where condition.

The condition for case query has filter on LastModifiedDate and we can not able to set last modified date in test class being it is one of the Audit field.

Not the answer you're looking for? Browse other questions tagged .