+ Start a Discussion
amateur1amateur1 

test case help

hi this is my controller and i am marking red where my test case is not covered

 

 

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];


}
  
}

the error i am getting is

 

System.QueryException: List has no rows for assignment to SObject for contact query 

 

this is my test case

 

@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();
}
}
}




Best Answer chosen by Admin (Salesforce Developers) 
Geetha ReddyGeetha Reddy
@istest
private class Netsmart_Portal_Homepage_Tc
{
static testMethod void validatetest1()
{
Account a = new Account();
            a.Name = 'Test Account';
            a.BillingState = 'AL';
            insert a;
        
            Contact c = new Contact();
            c.FirstName = 'Test';
            c.LastName = 'Contact';
            c.AccountId = a.Id;
            c.Email = 'contactemail@example.com';
            insert c;
           
Profile p = [select id from profile where name='Customer Portal Manager Custom']; 
User u = new User(alias = 'standt', email='standarduser@testorg.com', 
                  emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
                  localesidkey='en_US', profileid = p.Id, Contactid = c.id,
                  timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
            System.runAs(u) {
                  
            Account ac = new Account();
            ac.Name = 'Test Account';
            ac.BillingState = 'AL';
            insert ac;
        
            Contact c1 = new Contact();
            c1.FirstName = 'Test';
            c1.LastName = 'Contact';
            c1.AccountId = ac.Id;
            c1.Email = 'contactemail6@example.com';
            insert c1;
               
case c11=new case();
c11.Subject='aaa';
c11.Status='open';
c11.ContactId=c.id;
insert c11;
Netsmart_Portal_Homepage n1 =new Netsmart_Portal_Homepage();
}
}
}

 

All Answers

Sanjay33Sanjay33

try using public "List<contact> c{get;set;}" in place of "public contact c{get;set;}"

Geetha ReddyGeetha Reddy
@istest
private class Netsmart_Portal_Homepage_Tc
{
static testMethod void validatetest1()
{
Account a = new Account();
            a.Name = 'Test Account';
            a.BillingState = 'AL';
            insert a;
        
            Contact c = new Contact();
            c.FirstName = 'Test';
            c.LastName = 'Contact';
            c.AccountId = a.Id;
            c.Email = 'contactemail@example.com';
            insert c;
           
Profile p = [select id from profile where name='Customer Portal Manager Custom']; 
User u = new User(alias = 'standt', email='standarduser@testorg.com', 
                  emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
                  localesidkey='en_US', profileid = p.Id, Contactid = c.id,
                  timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
            System.runAs(u) {
                  
            Account ac = new Account();
            ac.Name = 'Test Account';
            ac.BillingState = 'AL';
            insert ac;
        
            Contact c1 = new Contact();
            c1.FirstName = 'Test';
            c1.LastName = 'Contact';
            c1.AccountId = ac.Id;
            c1.Email = 'contactemail6@example.com';
            insert c1;
               
case c11=new case();
c11.Subject='aaa';
c11.Status='open';
c11.ContactId=c.id;
insert c11;
Netsmart_Portal_Homepage n1 =new Netsmart_Portal_Homepage();
}
}
}

 

This was selected as the best answer
amateur1amateur1

i think u r going to be the next generation salesforce contributer thank u gettha mam hail reddy