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
ANIL PAKKIANIL PAKKI 

Hi, I know many of you might seen this error earlier: Failure Message: "System.ListException: List index out of bounds: 0", Failure Stack Trace, Stack Trace:Method7: line 133, column 1

public static testmethod void testuser()    {

      Account accountRecord1 = new Account(Name = 'Dell Products test',Core_Account_Type__c = 'Distributor',Core_Sales_Segment__c = 'Compute',Industry = 'Banking',BillingCity = 'Delhi',BillingCountry = 'USA',billingState = 'AK',billingStreet = 'Bratislava 1',Core_IPS_Active__c = true);
        insert accountRecord1;
        
        contact con = new contact(FirstName = 'Customer',LastName='john',Email='customer@john.com',AccountId=accountRecord1.id);
        insert con;
                      
        ApexPages.StandardController sc = new ApexPages.standardController(con);
        classcontroller obj = new classcontroller(sc); 
        
        List<User> userlst=[SELECT id,username,name,userroleid,contactid,profileid,Accountid,email,isactive from user where contactid=:con.id and profile.Name='Customer' ];
        Id CustRoleId=userlst[0].id;
        Test.starttest();
        obj.method(CustRoleId);
        Test.stoptest();      
        
        }  
SHAHNA MULLASHAHNA MULLA
This means that your query has returned no records that match the criteria.  If you then attempt to access an element at row 0, this will throw an error as that row doesn't exist.

Id CustRoleId=userlst[0].id;
Here the problem is with above line. 
userlst[] has no elements so trying to access the 0 index element will cause the list index out of bounds excpetion.

To protect against this you could change your code to first check to see if the returned List is not empty:
if (!userlst.isEmpty())
        {
         Id CustRoleId=userlst[0].id;
        }
 
Raj VakatiRaj Vakati
update the code as per the comments 
 
public static testmethod void testuser()    {


// Query the profile where name = 'Customer' 
  Account accountRecord1 = new Account(Name = 'Dell Products test',Core_Account_Type__c = 'Distributor',Core_Sales_Segment__c = 'Compute',Industry = 'Banking',BillingCity = 'Delhi',BillingCountry = 'USA',billingState = 'AK',billingStreet = 'Bratislava 1',Core_IPS_Active__c = true);
	insert accountRecord1;
	
	contact con = new contact(FirstName = 'Customer',LastName='john',Email='customer@john.com',AccountId=accountRecord1.id);
	insert con;
				  
				  
				  // create a user record with the profile name Customer and contact is the above contact
	ApexPages.StandardController sc = new ApexPages.standardController(con);
	classcontroller obj = new classcontroller(sc); 
	
	List<User> userlst=[SELECT id,username,name,userroleid,contactid,profileid,Accountid,email,isactive from user where contactid=:con.id and profile.Name='Customer' ];
	Id CustRoleId=userlst[0].id;
	Test.starttest();
	obj.method(CustRoleId);
	Test.stoptest();      
	
	}