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
Gab SilvermotionGab Silvermotion 

System.ListException: List index out of bounds: 0 in test class

Hi all, 

I have this error 2 times in this test class. 

System.ListException: List index out of bounds: 0
Class.FastSubmitAssetsClassTest.SubmitMultipleCaseTest: line 27, column 1

System.ListException: List index out of bounds: 0
Class.FastSubmitAssetsClassTest.SubmitSigleCaseTest: line 8, column 1

Code Sample :

@isTest(SeeAllData=true)
Public Class FastSubmitAssetsClassTest{


    //
    @IsTest Public Static Void SubmitSigleCaseTest(){
        FastSubmitAssetsClass FsA=new FastSubmitAssetsClass();
        FsA.SelectedId=FsA.assetContainers.get(0).cases.get(0).kase.id;           
        system.assertNotEquals(NULL,FsA.SubmitForApproval());
        
        PageReference submitPage=Page.FastSubmitAssetsPage;
        Test.setCurrentPageReference(submitPage);        
        ApexPages.CurrentPage().getParameters().put('assetId',FsA.assetContainers.get(0).asset.id);
        system.assertNotEquals(NULL,FsA.SubmitForApproval());
        
        //Action page testing
        PageReference actionPage=Page.FastSubmitActionPage;
        Test.setCurrentPageReference(actionPage);
        ApexPages.CurrentPage().getParameters().put('id',FsA.SelectedId);
        FastSubmitActionClass action=new FastSubmitActionClass();
        system.assertEquals(NULL,action.yes());
        system.assertNotEquals(NULL,action.no());
        
    }
    @IsTest Public Static Void SubmitMultipleCaseTest(){
        FastSubmitAssetsClass FsA=new FastSubmitAssetsClass();
        FastSubmitAssetsClass.CaseContainer cc=FsA.assetContainers.get(0).cases.get(0);
        cc.isSelected=true;           
        system.assertNotEquals(NULL,FsA.SubmitSelected());
        
        //Action page testing
        PageReference actionPage=Page.FastSubmitActionMultiplePage ;
        Test.setCurrentPageReference(actionPage);
        ApexPages.CurrentPage().getParameters().put('allids',cc.Kase.id);
        FastSubmitActionClass action=new FastSubmitActionClass();
        system.assertNotEquals(NULL,action.YesMulti());
        system.assertNotEquals(NULL,action.NoMulti());
        
        
    }
    //Product_udpate and OpportunityLine Item testing 
    @IsTest Public static void AdditionalTesting(){
        Product2 prod=[Select Id,Name from Product2 LIMIT 1];
        prod.Name='Name Changing';
        update prod;
        
        OpportunityLineItem oli=[SELECT id,PriceBookEntryId 
                                 FROM OpportunityLineItem LIMIT 1];
        UPDATE Oli;
        
    
    }

}



Gab SilvermotionGab Silvermotion
ok i noticed i had no products in my sandbox so i created one. now the error message has changed to 

FastSubmitAssetsClassTest.AdditionalTesting() Class 51 1 Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.FastSubmitAssetsClassTest.AdditionalTesting: line 51, column 1"
praveen murugesanpraveen murugesan
Hi Gab,

Could you please post you controller..

Thanks
pradeep naredlapradeep naredla
Hi gab,
   The error is because of you have written a query in the class and u entered the record in the test class which is not satisfing the condition of the query so it's not retreving the records into the list . Check that 

Thanks,
pradeep.