You need to sign in to do that
Don't have an account?
Dongzhi Yang
List Index Out Of Bounds Error in Test Class , But same code runs correctly in a seperate workspace.
Hi,
I have the following code which runs correct from a workspace in developer console. However, when i put them into a test class, the same code return the List Index Out Of Bounds Error. It is as if in a test class , no records can be retrieved.
Anyone who has the same experience? would appreciate very much for a solution
---------------------------------------------------------------
List<Warehouse_Transaction__c> w = [SELECT Name FROM Warehouse_Transaction__c WHERE Name = :'INV-0000'];
List<Merchandise__c> m = [SELECT Name FROM Merchandise__c WHERE Name = :'M-00010'];
system.debug(w[0].Name);
system.debug(m[0].Name);
I have the following code which runs correct from a workspace in developer console. However, when i put them into a test class, the same code return the List Index Out Of Bounds Error. It is as if in a test class , no records can be retrieved.
Anyone who has the same experience? would appreciate very much for a solution
---------------------------------------------------------------
List<Warehouse_Transaction__c> w = [SELECT Name FROM Warehouse_Transaction__c WHERE Name = :'INV-0000'];
List<Merchandise__c> m = [SELECT Name FROM Merchandise__c WHERE Name = :'M-00010'];
system.debug(w[0].Name);
system.debug(m[0].Name);
give this and test it
All Answers
http://help.salesforce.com/apex/HTViewSolution?id=000181121&language=en_US
---------following code runs correctly in a workspace-------------------------
Account[] w = [SELECT Name FROM Account limit 10];
system.debug(w[0].Name);
--------------------------------------------------------------------------
-------------------however, if put in a test class the code return List Index Out of Bound Error at the highlighted line---------------
@isTest
public class Test_triAccount{
static testMethod void triAccount(){
Account[] w = [SELECT Name FROM Account limit 10];
system.debug(w[0].Name);
}
}
give this and test it