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
ran67ran67 

Testcase Help

Hi

I have written a controller .and i wrote a testcase for it but i less code cover 

can u please help me. which are in red color in controller the code has not covered...........

Thanks in advances

I am sending controller and testcase

Mycontroller

public class MyChecklist_All

                  {

                 

                  public Opportunity[] opp ;

                  public MyChecklist__c[] chklist{get;set;}

                  public MyChecklist__c[] chklistapprove{get;set;}

                  public MyChecklist__c[] chklistrefuse{get;set;}

                  Set<Id> oppid = new Set<Id>();

                  Public Contact Con {get; set;}

                  Public Account Ac {get; set;}

                 

                  public user u{get;set;}

                  public MyChecklist_All()

                  {

                 

                  u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()];

                  Con=[select id,lastname from Contact where id =: u.ContactId];

                  Ac = [select id,lastname from Account where PersonContactId =: con.Id];

                  opp=[select id from Opportunity where Accountid=:ac.id];

                  for(Opportunity aa: opp)

                   {

                   oppid.add(aa.id);

                   }

                  chklist=[select id,Name,Opportunity_Name__r.Account.name,Product_Name__c,Approved_On__c,Category__c,Comments__c,Checklist_Customized_By__c,Status__c from MyChecklist__c where Opportunity_Name__c IN: oppid

                  and Active__c=true];

                 

                  chklistapprove=[select id,Name,Opportunity_Name__r.Account.name,Product_Name__c,Approved_On__c,Category__c,Comments__c,Checklist_Customized_By__c,Status__c from MyChecklist__c where Opportunity_Name__c IN: oppid

                  and Approval_Status__c='Accepted'];

                 

                  chklistrefuse=[select id,Name,Opportunity_Name__r.Account.name,Product_Name__c,Approved_On__c,Category__c,Comments__c,Checklist_Customized_By__c,Status__c from MyChecklist__c where Opportunity_Name__c IN: oppid

                  and Approval_Status__c='Refused'];

                 

                  }

                  }

TEstcase:

@istest
private class MyChecklist_All_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;



opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


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=a.id;
c1.Email = 'contactemail@example.com';
insert c1;


opportunity o1 = new opportunity();
o1.name='test';
o1.StageName='prospecting';
o1.CloseDate=date.valueof('2011-10-05');
o1.Accountid=ac.id;
insert o1;
MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Opportunity_Name__c=o1.id;
insert m1;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

 

 

 

 


kiranmutturukiranmutturu

@istest
private class MyChecklist_All_Tc
{
static testMethod void validatetest1()
{


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, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {
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;

 

opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Opportunity_Name__c=o.id;
insert m1;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

 

try this

 

ran67ran67

code has not at all covered one percentage .

imranrazaimranraza

Hi ran67,

use this code:-

@istest
private class TestMyChecklist_All
{
static testMethod void runTest()
{


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, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {
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;

 

opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Active__c=true;
m1.Opportunity_Name__c=o.id;
insert m1;

MyChecklist__c m2=new MyChecklist__c();
m2.name='Test';
m2.Active__c=true;
m2.Opportunity_Name__c=o.id;
m2.Approval_Status__c='Accepted';
insert m2;

MyChecklist__c m3=new MyChecklist__c();
m3.name='Test';
m3.Active__c=true;
m3.Opportunity_Name__c=o.id;
m3.Approval_Status__c='Refused';
insert m3;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

ran67ran67

code is notcovered in my account i have person account .how can i declare it

thanks in advances

imranrazaimranraza

Replace this code Profile p = [select id from profile where name='System Administrator']; with the below line

Profile p = [select id from profile where name='System Administrator'];

and add Debug log and check Is there any Exception in code?

ran67ran67

acutually i dont have exception with profile.

in account object i have personaccount recordtype.

then how i can i declare in testcase

 

thanks in advances

kiranmutturukiranmutturu

then  get  the recordtype of that object and assign the same instead of query u can get the recordtype id u can get like this

 

private static final Id RECORD_TYPE_NAME_RECORD_TYPE_ID =
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();

imranrazaimranraza

RecordType rt = [SELECT id from RecordType where name ='PersonAccount' and SObjectType='Account' limit 1];

if(rt != null){

a.RecordTypeId = rt.id;

}

 

place this code where you are creating account.

ran67ran67

still am not able to cover the code

 

kiranmutturukiranmutturu

post the updated test class once what u are checking now....

ran67ran67

@istest
private class MyChecklist_All_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;



opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


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=a.id;
c1.Email = 'contactemail@example.com';
insert c1;


opportunity o1 = new opportunity();
o1.name='test';
o1.StageName='prospecting';
o1.CloseDate=date.valueof('2011-10-05');
o1.Accountid=ac.id;
insert o1;
MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Opportunity_Name__c=o1.id;
insert m1;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

 

kiranmutturukiranmutturu

just try with standard user profile once

 

@istest
private class MyChecklist_All_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;

 

opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


Profile p = [select id from profile where name='standard user'];
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 = 'contactemail@example.com';
insert c1;


opportunity o1 = new opportunity();
o1.name='test';
o1.StageName='prospecting';
o1.CloseDate=date.valueof('2011-10-05');
o1.Accountid=ac.id;
insert o1;
MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Opportunity_Name__c=o1.id;
insert m1;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

imranrazaimranraza

Use this code we were missing this lien:- a1. PersonContactId = con.id; because of this code coverage was 0.

@istest
private class TestMyChecklist_All
{
static testMethod void runTest()
{

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',ContactId=con.id,
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {

Account a1 = new Account();
a1.Name = 'Test Account';
a1.BillingState = 'AL';
a1. PersonContactId = con.id;
insert a1;

opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Active__c=true;
m1.Opportunity_Name__c=o.id;
insert m1;

MyChecklist__c m2=new MyChecklist__c();
m2.name='Test';
m2.Active__c=true;
m2.Opportunity_Name__c=o.id;
m2.Approval_Status__c='Accepted';
insert m2;

MyChecklist__c m3=new MyChecklist__c();
m3.name='Test';
m3.Active__c=true;
m3.Opportunity_Name__c=o.id;
m3.Approval_Status__c='Refused';
insert m3;

MyChecklist_All s1= new MyChecklist_All();

}
}
}

ran67ran67

hi

i was using   in customer portal profile only so only i have declared like that..

if i declare standard user then  zero percentage code was covered 

actual my problem nt in profile

actually in mycontroller i have been used personcontactid=:c.id;

where c is the contact 

how to declare the personaccount in salesforce

 

thanks in advance

 

kiranmutturukiranmutturu

this is how you can create a person account 

 

Account ac = new Account();
ac.Name = 'Test Account';
ac.BillingState = 'AL';
ac.recodtype = Schema.SObjectType.Account.getRecordTypeInfosByName().get('person').getRecordtypeid();
insert ac;

 

note: red colored name should be ur person account record type name... in my instance it is person....