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
Uday KUday K 

Test coverage for the controller class

Hello all,

Please help me how to cover the red highlighted block below.

 

I am getting the 70% coverage in the test class. The constructor block is not getting covered. Can you please help me.

 

public class CLS_AccountEditPage {
public String Account { get; set; }
Public String CompanyType{get;set;}
Public String AccountName{get;set;}
Public String Industry{get;set;}
Public String AccountSiteCity{get;set;}
Public String Phone{get;set;}
Public String Website{get;set;}
Public String ParentAccount{get;set;}
Public String AccountSiteLocation{get;set;}
public Account acc{get;set;}
public string surl = '';
public id aid{get;set;}
   
public CLS_AccountEditPage ()
{
acc=new account();
if(ApexPages.currentPage().getUrl() != null)
  {
    surl = ApexPages.currentPage().getUrl();
    if(surl.contains('001'))
   {
    aid= ApexPages.currentPage().getParameters().get('id');
    if(aid!=null  && aid!='')
    {
    acc=[select name,Industry,phone,Website,Account_Site_Location__c,Company_Type__c,Account_Site_City__c  from Account where id=:aid];
    AccountName=acc.name;
    Industry=acc.Industry;
    phone=acc.phone;
    Website=acc.Website;
    CompanyType=acc.Company_Type__c;
    AccountSiteCity=acc.Account_Site_City__c;
    AccountSiteLocation=acc.Account_Site_Location__c;
   }
    }
}
}
public  PageReference insertdata()
{
try{
acc.Name=AccountName;
acc.Industry=Industry;
acc.Phone=Phone;
acc.Website=Website;
acc.Company_Type__c=CompanyType;
acc.Account_Site_Location__c=AccountSiteLocation;
acc.Account_Site_City__c=AccountSiteCity;
insert acc;
}
catch(Exception e){
update acc;
}
PageReference pr=new PageReference('/'+acc.id);
return pr;
}
}

Test class;
@isTest
Public class Test_Cls_ContactEdit
{
   Public Static testMethod void AccountEdit()
   {
      Cls_AccountEdit cc = new Cls_ContactEdit();
      cc.title = 'Mr.';
      cc.FirstName = 'Swamy';
      cc.LastName = 'Penchal';
      cc.leadsource = 'Other';
      cc.Email = 'test@testorg.com';
      cc.Mobile = '5468745478';
      cc.Phone = '8487484568';
      cc.Description = 'Good';
      cc.insertdata();
      
      PageReference pageRef = Page.VF_AccounteditPage;
      Test.setCurrentPage(pageRef);
      Cls_ContactEdit ccc = new Cls_ContactEdit();
      ccc.LastName = 'Penchala';
      ApexPages.currentPage().getUrl();
      String s = cc.Save().getUrl();
      ccc.surl = '001';
      String c = ApexPages.currentPage().getParameters().put('id','001Z000000KRcRe');
      String d = ApexPages.currentPage().getParameters().get('id');
      System.debug('@@@@@@'+s+'$$$$$$'+c+'!!!!!!'+d);
      ccc.cid = '001Z000000KRcRe';
      System.debug('@@@@@@'+s);
      ccc.insertdata();
      ccc.Cancel();
   }
}


Thanks,

Uday

 

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

In your test method try something like this:

 

PageReference pageRef = new PageReference('/Apex/VF_AccounteditPage?id=001XYZEDG');

 

Your Page Reference does not have '001' in the URL per your IF Statement. When you do your page reference you are just giving it the VF page wihtout any parameters which whoulc include the 001 string.

 

Also, FYI you can bind directly to SObjects in your VF Page. So instead of having 10 lines of code where you set CompanyType, AccountName, etc. YOu could just bind directly to acc.Name like this:

 

<Apex:inputField value = {!acc.CompanyType}>

 

and in your controller you wouldn't need to do these line:

    AccountName=acc.name;
    Industry=acc.Industry;
    phone=acc.phone;
    Website=acc.Website;
    CompanyType=acc.Company_Type__c;
    AccountSiteCity=acc.Account_Site_City__c;
    AccountSiteLocation=acc.Account_Site_Location__c;

 

 

 

All Answers

Cory CowgillCory Cowgill

In your test method try something like this:

 

PageReference pageRef = new PageReference('/Apex/VF_AccounteditPage?id=001XYZEDG');

 

Your Page Reference does not have '001' in the URL per your IF Statement. When you do your page reference you are just giving it the VF page wihtout any parameters which whoulc include the 001 string.

 

Also, FYI you can bind directly to SObjects in your VF Page. So instead of having 10 lines of code where you set CompanyType, AccountName, etc. YOu could just bind directly to acc.Name like this:

 

<Apex:inputField value = {!acc.CompanyType}>

 

and in your controller you wouldn't need to do these line:

    AccountName=acc.name;
    Industry=acc.Industry;
    phone=acc.phone;
    Website=acc.Website;
    CompanyType=acc.Company_Type__c;
    AccountSiteCity=acc.Account_Site_City__c;
    AccountSiteLocation=acc.Account_Site_Location__c;

 

 

 

This was selected as the best answer
Uday KUday K

Hello ,

 

Thanks a lot. Worked out the the logic

 

Thanks,

Uday