You need to sign in to do that
Don't have an account?

vf page test classes
I am new to Vf and test class. Can anyone please help me in test class for following Visual Force. Requirement is to display all Account record and when clicked on Account Name Related contact must be displayed.
Following is the VF PAge and Controller
-----------------------------------------------------------------------------------VF-----------------------------------------------------------------
<apex:page standardController="Account" extensions="SearchAccountExtensions1" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:outputPanel id="table">
<br/>
<h1> Account List</h1>
<br/>
<br/>
<apex:outputPanel rendered="{! renderTable }">
<apex:pageBlockTable value="{! accountList }" var="account">
<apex:column headerValue="Account Name">
<apex:outputLink value="/apex/RelatedContactSearch?AccId={!account.id}">
{!account.name}
</apex:outputLink>
</apex:column>
<apex:column value="{!account.Name}"/>
<apex:column value="{!account.Phone}"/>
<apex:column value="{!account.BillingStreet}"/>
<apex:column value="{!account.BillingCity}"/>
<apex:column value="{!account.BillingState}"/>
<apex:column value="{!account.BillingPostalCode}"/>
<apex:column value="{!account.BillingCountry}"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------------------------------------------------------------Controller------------------------------------------------------------
public class SearchAccountExtensions1 {
public SearchAccountExtensions1(ApexPages.StandardController controller) {
this();
}
public List<Account> accountList{ get;set; }
public String inputString{get;set;}
public Boolean renderTable{get;set;}
/**
This is constructor of extension which initialize initial rendering of Account Table.
*/
public SearchAccountExtensions1() {
renderTable = true;
inputString = ApexPages.currentPage().getParameters().get('input');
if( inputString == null ) {
accountList = [
SELECT
Name,
Phone,
BillingStreet, BillingCity, BillingState, BillingPostalCode,BillingCountry
FROM
Account
];
}
}
}
Following is the VF PAge and Controller
-----------------------------------------------------------------------------------VF-----------------------------------------------------------------
<apex:page standardController="Account" extensions="SearchAccountExtensions1" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:outputPanel id="table">
<br/>
<h1> Account List</h1>
<br/>
<br/>
<apex:outputPanel rendered="{! renderTable }">
<apex:pageBlockTable value="{! accountList }" var="account">
<apex:column headerValue="Account Name">
<apex:outputLink value="/apex/RelatedContactSearch?AccId={!account.id}">
{!account.name}
</apex:outputLink>
</apex:column>
<apex:column value="{!account.Name}"/>
<apex:column value="{!account.Phone}"/>
<apex:column value="{!account.BillingStreet}"/>
<apex:column value="{!account.BillingCity}"/>
<apex:column value="{!account.BillingState}"/>
<apex:column value="{!account.BillingPostalCode}"/>
<apex:column value="{!account.BillingCountry}"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------------------------------------------------------------Controller------------------------------------------------------------
public class SearchAccountExtensions1 {
public SearchAccountExtensions1(ApexPages.StandardController controller) {
this();
}
public List<Account> accountList{ get;set; }
public String inputString{get;set;}
public Boolean renderTable{get;set;}
/**
This is constructor of extension which initialize initial rendering of Account Table.
*/
public SearchAccountExtensions1() {
renderTable = true;
inputString = ApexPages.currentPage().getParameters().get('input');
if( inputString == null ) {
accountList = [
SELECT
Name,
Phone,
BillingStreet, BillingCity, BillingState, BillingPostalCode,BillingCountry
FROM
Account
];
}
}
}
Please try the below code, it is working fine. Kindly modify the code as per your requirement.
Visualforce:
Controller:
Test Class:
Please refer below link for Test Classes Best Practice :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
I hope it helps you.
Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
You can try the following code :
VF page :
<apex:page controller="SearchAccountExtensions1">
<apex:form id="frm">
<apex:pageBlock id="pgblk">
<apex:pageblockSection title="Accounts" id="pbsec">
<apex:pageBlockTable value="{!accountList}" var="account" id="pgtable">
<apex:column headervalue="Account name">
<apex:commandLink value="{!account.Name}" ReRender="contactBlock">
<apex:actionSupport event="onclick" action="{!getRelatedContact}" rerender="conpgblk" >
<apex:param name="accountId" assignTo="{!accountId}" value="{!account.Id}"/>
</apex:actionSupport>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
<apex:pageBlock id="conpgblk">
<apex:pageblockSection title="Contacts">
<apex:pageBlockTable value="{!contactList}" var="contact">
<apex:column headervalue="First Name" value="{!contact.FirstName}" />
<apex:column headervalue="Last Name" value="{!contact.lastName}" />
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :
public class SearchAccountExtensions1 {
public List<Account> accountList{get;set;}
public Id accountId{get;set;}
public list<contact> contactList{get;set;}
public SearchAccountExtensions1()
{
accountList=new List<Account>();
contactList=new List<Contact>();
accountList = [SELECT Id,Name FROM Account LIMIT 10];
}
public void getRelatedContact()
{
contactList = [SELECT Id,firstName,LastName FROM Contact WHERE AccountId =:accountId LIMIT 1000];
}
}
Test Class :
@isTest
Private class SearchAccountExtensions1_Test {
@isTest static void notMoreThanFour_Test() {
List<Account> accountList = new List<Account>();
for(Integer i=0;i<10;i++)
{
Account acc = new Account();
acc.Name = 'skg'+i;
accountList.add(acc);
}
insert accountList;
List<Contact> contactList = new List<Contact>();
for(Integer i=0;i<10;i++)
{
Contact con = new Contact();
con.FirstName = 'skg'+i;
con.LastName = 'sk'+i;
con.AccountId = accountList[i].id;
contactList.add(con);
}
insert contactList;
test.startTest();
SearchAccountExtensions1 conObj = new SearchAccountExtensions1();
conObj.getRelatedContact();
system.assertEquals(10, contactList.size());
test.stopTest();
}
}
Please mark as best answer if it helps you.
Thank You
Ajay Dubedi