You need to sign in to do that
Don't have an account?
morganeCRMCLOUDER
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 ?
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
@isTest(SeeAllData=true)
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