You need to sign in to do that
Don't have an account?
Vamsi
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
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; } }
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
I tried that tooo but it doesn't return any records ..!!!
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.
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 .!!