You need to sign in to do that
Don't have an account?
System.ListException: List index out of bounds: 1
HI...
this my testclass.
@isTest
private class testloadinginsamepage
{
static testMethod void Mypage_test()
{
Accountdetails ad = new Accountdetails();
String Aname ='GenePoint';
System.assertEquals('GenePoint',Aname); // here success
List<contact> cn = ad.getcodetails();
List<Contact> cn1 = [select LastName from contact where Account.name = 'GenePoint' limit 1];
System.assertEquals('Edna Fra',cn1[0].LastName);
}
}
This is my apex class.
public class Accountdetails
{
public String Aname {get; set;}
public List<contact> getcodetails()
{
cc =[select name ,Email from Contact where Account.name=:Aname ];
return cc;
}
}
Can any one please tell me where i am doing the mistake.
your are trying to access cn1 [0] but before you need to check if you have the list of records or not
List<Contact> cn1 = [select LastName from contact where Account.name = 'GenePoint' limit 1];
if(!cn1.isEmpty())
System.assertEquals('Edna Fra',cn1[0].LastName);
You have not set the aname in your class. It should have been
ad.Aname = GenePoint';
So i think this assert fails.
System.assertEquals('Edna Fra',cn1[0].LastName);
Hi kiran,
here, i did like this
List<Contact> cn1 = [select LastName from contact where Account.name ='GenePoint' limit 1];
if(!cn1.isEmpty()){
System.assertEquals('Edna Fra',cn1[0].LastName);// here test must fail.
}
Now there is not error but according to assert equals i gave a wrong contact name , so test must fail but it is showing test success.