+ Start a Discussion
VamsiVamsi 

How to write test class for a method returning a set of records

Hi,

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%. Not sure how to verify the results 

In the below class I am using custom labels to store the number of hours 
Apex class
 
public with sharing class UnclaimCases
{
    public list<case> caseami {get;set;}
    public string strhours = Label.DateFilter;
    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 UnclaimedAMICases()
    {
     
     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;
    }
    

}

 
cloudstreamercloudstreamer
Hi,

Do you want to test if your query is returning the records which is saved in the instance variable caseami  in the testclass?
If that is the case , instantiate the class in the test method, use the instance of the class to invoke inc() method, then invoke the getter method of caseami instance variable to get the value in caseami in the test method.
I think this will work.

Thanks



 
VamsiVamsi
Hi,

I tried that tooo but it doesn't return any records ..!!! 

 
cloudstreamercloudstreamer
I was able to get the concept  working. Take a look at the following code.
public with sharing class ReadAccounts
{
    Datetime currenttimeami = system.now(); 
    public List<Account> accountsQueried {get;set;}
    public ReadAccounts()
    {
     
     accountsQueried = [Select id,name from Account where createdDate < :currenttimeami LIMIT 10000 ];
         system.debug('accountsQueried' + accountsQueried );
         system.debug(' current datetime value ' + currenttimeami );
    }
    
    public pagereference readingAccounts()
     {
         Datetime currenttimeami = system.now();
        accountsQueried = [Select id,name from Account where createdDate < :currenttimeami LIMIT 10000 ];
         system.debug('accountsQueried' + accountsQueried );
         system.debug(' current datetime value ' + currenttimeami );
        
        return null;
    }
    

}

_______________________________________________________________________________________________________
@isTest
public class ReadAccountTest {
    static testMethod void testReadAccount(){    
        // Set up the Account record.
    Account a = new Account(Name='Test Account');
    insert a;
        test.startTest();
        ReadAccounts ra= new ReadAccounts();
        List<Account> acc= ra.accountsQueried;
        system.assertNotEquals(1, acc.size());
        test.stopTest();
    }


}

Thanks

Please mark as best answer if your issue is resolved.
 
cloudstreamercloudstreamer
Hi The code in the test method returns the record that I created in the test method ( Test Data). Ideally, to test any test case, we should create test data as a best practice. In your case, you should create test data which satisfies the conditions in your query.That is , create test data in such a way that you know that when the query is executed as a part of the the call from the test method, these test data records will be returned. I created 1 account record in my test class and when I initiated the class , the constructor is invoked and the code within runs and the variable is set with one record ( test data record). Hence my system.assert statement returns true. You could also enhance the test method to include some assert statements to check if the returned records are the test data records that you created that satisfy the condition. You can also create some test data which do not satisfy the conditions in your query and verify using system.assert statements that these are not being returned thus validating your query ( which is the only thing you can test for your code since you do not have any other business logic in there). Salesforce provides code coverage to a snippet of code if it is being called from any test method. But that does not prove that the functionality that you have written with that code snippet is working. Best practice for writing test methods for any class as you know is to create test data for both positive and negative test cases and border conditions and test your code with these different sets of test data and use system.assert statements to validate your results. reference articles https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods?language=en https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests I hope this answers your question. Thanks
VamsiVamsi
Thank you for the information. 

But in my class I have a condition in query as LastModifiedDate >: acttimeami  AND LastModifiedDate <: currenttimeami. It would return only the cases modified last 24hours. Then how can we create test records for this and perform testing .!!