+ Start a Discussion
Dongzhi YangDongzhi 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);
Best Answer chosen by Dongzhi Yang
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
@isTest(SeeAllData=true)
give this and test it

All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
This will help you i guess
http://help.salesforce.com/apex/HTViewSolution?id=000181121&language=en_US
Dongzhi YangDongzhi Yang
 I have made the following test using Standard object, and got the same error


---------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);
    }
}
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
@isTest(SeeAllData=true)
give this and test it
This was selected as the best answer
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Use system.assert() method to check in test class