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
aressaress 

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  
            ];
        }
    }
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ayisha,

Please try the below code, it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AccountContactC" sidebar="false">
    <apex:form >
        <apex:pageBlock title="AccountTable">
            <apex:pageBlockTable value="{!Acclst}" var="A">
                <apex:column headerValue="NAME OF THE ACCOUNT" > 
                    <apex:commandLink value="{!A.Name}" action="{!setupContacts}" rerender="conttable">
                        <apex:param value="{!A.Id}" name="idForConts" assignTo="{!recid}"/>
                    </apex:commandLink>
                </apex:column>  
                <apex:column value="{!A.Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock title="Contacts">
        <apex:pageBlockTable value="{!contacts}" var="contact" id="conttable">
            <apex:column value="{!contact.FirstName}"/>
            <apex:column value="{!contact.LastName}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


Controller:
public class AccountContactC {
    
    public string recid{get;set;}    
    public list<Account> Acclst{get;set;}
    public List<Contact> contacts {get; set;}
    
    public AccountContactC(){
        Acclst = [select Id,Name from Account LIMIT 10];
        contacts=null;
    }
    
    public void setupContacts() {
        contacts=[select id, FirstName, LastName from Contact where AccountId=:recId];
    }
}

Test Class:
@isTest
public class TestAccountContactC {

    static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='SFDC' ;
		INSERT testAccount;
		
		Contact con = new Contact();
		con.FirstName='Khan';
		con.LastName='Anas';
		con.Accountid= testAccount.id;
		INSERT con;
		
		Test.StartTest(); 
			AccountContactC obj = new AccountContactC();
			obj.setupContacts();
			
		Test.StopTest();
	}
}


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
Ajay K DubediAjay K Dubedi
Hi Ayisha,

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