You need to sign in to do that
Don't have an account?

Testcase issue coverage
public class Netsmart_Portal_Homepage{ public list<case> opencaserecords{get;set;} public list<case> closedcaserecords{get;set;} public list<case> caserecords{get;set;} public user u{get;set;} public contact c{get;set;} public Netsmart_Portal_Homepage(){ u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()]; c = [Select LastName,id from Contact where id =:u.Contactid]; caserecords=[select id,CaseNumber,Subject,Status, Priority ,CreatedBy.FirstName,CreatedDate,CreatedBy.id,ClosedDate from case where ContactId=:c.id ]; closedcaserecords=[select id,CaseNumber,Subject,Status, Priority ,CreatedDate,CreatedBy.FirstName,CreatedBy.id,ClosedDate from case where ContactId=:c.id and isclosed=true]; opencaserecords=[select id,Subject,CaseNumber,Status, Priority ,CreatedDate,CreatedBy.FirstName,CreatedBy.id,ClosedDate from case where ContactId=:c.id and isclosed=false]; } }
Unable to cover this line: c = [Select LastName,id from Contact where id =:u.Contactid];
list has no rows im getting error
this is my testcase:
@istest
private class Netsmart_Portal_Homepage_Tc
{
static testMethod void validatetest1()
{
Account a = new account();
a.name = 'Test';
a.Type = 'New Customer';
a.BillingState = 'AL';
a.recordtypeid='0127000000014u3';
insert a;
user u=[select id,contactid from user where isactive=true limit 1];
System.RunAs(u){
contact c1=[select id from contact where id=:u.contactid];
case c11=new case();
c11.Subject='aaa';
c11.Status='open';
c11.ContactId=c1.id;
c11.AccountId =a.id;
insert c11;
Netsmart_Portal_Homepage n1 =new Netsmart_Portal_Homepage();
}
}
}
All Answers
Hello,
The reason you are not getting any records returned for that query is because you haven't created any for this user. And since Spring 12 release, by default test coverages do not consider your live records. You need to create the records!
user u=[select id,contactid from user where isactive=true limit 1];
System.RunAs(u){
//Before querying you have to create a contact while running as this user.
Contact c = new Contact();
c.LastName = 'test';
c.AccountId = yourAccount.Id; // whichever account you have created
// also add any other fields if required.
insert c;
contact c1=[select id from contact where id=:u.contactid]; // now your query should return you a record!
case c11=new case();
c11.Subject='aaa';
c11.Status='open';
c11.ContactId=c1.id;
c11.AccountId =a.id;
insert c11;
Netsmart_Portal_Homepage n1 =new Netsmart_Portal_Homepage();
}
Hi
Try to insert the one contact in test method.
Regards,
Rajesh.