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
amateur1amateur1 

test case help

this is my controller and test case 

 

it got 65 percent plz help

 

 

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;}
public MyChecklist__c[] chkrefused{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,Product_Checklist__r.name,Approved_On__c,Received__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Active__c=true];
chklistapprove=[select id,Name,Product_Checklist__r.name,Approved_On__c,Received__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c='Accepted'];
chklistrefuse=[select id,Name,Product_Checklist__r.name,Received__c,Approved_On__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c != 'Refused' and Approval_Status__c != 'Accepted'];
chkrefused=[select id,Name,Product_Checklist__r.name,Received__c,Approved_On__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c = 'Refused'];
}



testMethod static void testWebSrvceAddNumbers()
{


account a =new account();
a.Preferred_Delivery_Address__c='Home';
a.E_mail__c='sandeep@tvarana.com';
a.name='dsds';
//a.ts2__Lastname__pc='dds';
insert a;




contact c=new contact();
c.ts2__Lastname__c='dds';
c.LastName='dds';
c.accountid=a.id;
c.CurrencyIsoCode='INR';
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'); 
                  
      //  user u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()];
         system.runas(u)
         {
         
         account a1 =new account();
a1.Preferred_Delivery_Address__c='Home';
a1.E_mail__c='sandeep@tvarana.com';
a1.name='dds';
//a1.PersonContactId=c.id;
insert a1;
         
                

MyChecklist_All m=new MyChecklist_All();
   }

   

}
}

Niket SFNiket SF

Please try with this,

 

testMethod static void testWebSrvceAddNumbers()
{
    account a =new account();
    a.Preferred_Delivery_Address__c='Home';
    a.E_mail__c='sandeep@tvarana.com';
    a.name='dsds';
    //a.ts2__Lastname__pc='dds';
    insert a;
    
    contact c=new contact();
    c.ts2__Lastname__c='dds';
    c.LastName='dds';
    c.accountid=a.id;
    c.CurrencyIsoCode='INR';
    insert c;
    
    Opportunity objNewOpp = new Opportunity(Name ='Test',CloseDate=System.today(),Stage='Initial',AccountId =a.Id);
    insert objNewOpp;
    
    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');
                  
      //  user u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()];
         system.runas(u)
         {
         
         account a1 =new account();
a1.Preferred_Delivery_Address__c='Home';
a1.E_mail__c='sandeep@tvarana.com';
a1.name='dds';
//a1.PersonContactId=c.id;
insert a1;
         
                

MyChecklist_All m=new MyChecklist_All();
   }

   

}
}

 

If it helps you, Please mark as solved

 

Thank you

Nik

amateur1amateur1

hi i am getting 65 with list has no  rows for account query which is not getting covered plz help how to cover this query

 

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

Niket SFNiket SF

You need to create the persone Account in the test method only. It will solve your problem

 

a1.RecordTypeId = PERSON_ACCOUNT_RECORD_TYPE_ID;

amateur1amateur1

hi nick i tried all possible ways but unable to cover the account query can u please give me the test class for my controller how to create personaccoutid and all please give me the code