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
morganeCRMCLOUDERmorganeCRMCLOUDER 

List remains empty after query (but the query works)

Hello, I should have missed something about lists from queries. Here is what I have :

 

@isTest 
private class MT_Production_Test{ 

static testMethod void validate_Production() { 
  List production = [SELECT name FROM Production__c ]; 
  integer listSize= production.size(); 
  System.debug('size of the list ' + listSize); 

}

 

 When I check the file for the result of the test, it shows up "0" for the size (though the file compiles, and the query in itself works (getting 3 rows). Why ? Where am I wrong ?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is because test classes by default don't see the organization data.  Thus although there are three Production__c records in the system, they are not available to your unit tests.

 

You have two choices:

 

(1) Create the data that you will query back as part of the test - this is the preferred option. So in your case you'd insert a test Production__c record.

 

(2) Allow the test to see all of the data in the system - the problem here is that if you run the test on a newly created sandbox and it expects some data to be present, it will fail.  There are some cases where you have to access the real data - default price book for example.

 

There's more information on this at:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

All Answers

Marko LamotMarko Lamot
@isTest(SeeAllData=true)

bob_buzzardbob_buzzard

This is because test classes by default don't see the organization data.  Thus although there are three Production__c records in the system, they are not available to your unit tests.

 

You have two choices:

 

(1) Create the data that you will query back as part of the test - this is the preferred option. So in your case you'd insert a test Production__c record.

 

(2) Allow the test to see all of the data in the system - the problem here is that if you run the test on a newly created sandbox and it expects some data to be present, it will fail.  There are some cases where you have to access the real data - default price book for example.

 

There's more information on this at:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

This was selected as the best answer
morganeCRMCLOUDERmorganeCRMCLOUDER
it works with the @isTest(SeeAllData=true) . Thank you for all the explanations !