function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sujin h ssujin h s 

How to test a method that is returning records based on the query

I have a method in apex class which is accepting 2 parameters. The method is returning  some records based on the query(2 parameters are using in where clause of the query) in that method.
Can anyone help me how to write a test case(code for test method) for that method?
Best Answer chosen by sujin h s
SwethaSwetha (Salesforce Developers) 
HI Sujin,

It is not a recommended practice to use SeeAll data= True. This would query data in real time. This implies your code coverage would drop when deploying to production org.

Related: https://salesforce.stackexchange.com/questions/137836/test-class-for-a-method-that-returns-collection-of-records

Thanks

All Answers

RituSharmaRituSharma
You will need to create records with relevant field values. Set field values as per where clause of your query.
sujin h ssujin h s
Is it a wrong way of testing?
If we set (SeeAllData=True)
Test data= retrieved from workbench based on query
Returned data(By apex class) = allowing the class to see the org data by setting  SeeAllData= True and retrive data using query
And comparing it against each other (Test Data Vs Returned Data)
SwethaSwetha (Salesforce Developers) 
HI Sujin,

It is not a recommended practice to use SeeAll data= True. This would query data in real time. This implies your code coverage would drop when deploying to production org.

Related: https://salesforce.stackexchange.com/questions/137836/test-class-for-a-method-that-returns-collection-of-records

Thanks
This was selected as the best answer