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
streetstreet 

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

 

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

vishal@forcevishal@force

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

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Try to insert the one contact in test method.

 

 

Regards,

Rajesh.

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