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

how to write test classes for visualforcepages please help me
Below is the code how can we write test class for this page with controller. Please help me
<apex:page controller="listViewsForContactRecords">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!accountsToDisplay}" var="acc" columns="2" id="pgtparent">
<apex:column headerValue="AccountName" >
<apex:commandLink value="{!acc.name}" action="{!MethodToCall}" rerender="pgbContacts" status="status">
<apex:param value="{!acc.Id}" name="idForConts" assignTo="{!recid}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!acc.Phone}"/>
</apex:pageblockTable>
</apex:pageBlock>
<apex:pageBlock title="Account Related Contacts">
<apex:outputPanel id="pgbContacts">
<apex:actionStatus startText="Please Wait Loading..." stopText="" id="status"></apex:actionStatus>
<apex:pageblockTable value="{!conList }" var="cons">
<apex:column headerValue="FirstName">
<apex:inputText value="{!cons.FirstName}"/>
</apex:column>
<apex:column headerValue="LastName">
<apex:inputText value="{!cons.LastName}"/>
</apex:column>
<apex:column >
<apex:commandButton value="EditContact"/>
</apex:column>
</apex:pageblockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class listViewsForContactRecords {
public list<contact> conList{get;set;}
public string recid{get;set;}
set<id> accIds = new set<id>();
public list<account> accountsToDisplay{get;set;}
public account getAcounts() {
return null;
}
public listViewsForContactRecords(){
accountsToDisplay = [select id, name, phone from account limit 10];
}
public void MethodToCall() {
conList = [select id, FirstName, LastName from contact where accountid=:recid];
}
}
<apex:page controller="listViewsForContactRecords">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!accountsToDisplay}" var="acc" columns="2" id="pgtparent">
<apex:column headerValue="AccountName" >
<apex:commandLink value="{!acc.name}" action="{!MethodToCall}" rerender="pgbContacts" status="status">
<apex:param value="{!acc.Id}" name="idForConts" assignTo="{!recid}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!acc.Phone}"/>
</apex:pageblockTable>
</apex:pageBlock>
<apex:pageBlock title="Account Related Contacts">
<apex:outputPanel id="pgbContacts">
<apex:actionStatus startText="Please Wait Loading..." stopText="" id="status"></apex:actionStatus>
<apex:pageblockTable value="{!conList }" var="cons">
<apex:column headerValue="FirstName">
<apex:inputText value="{!cons.FirstName}"/>
</apex:column>
<apex:column headerValue="LastName">
<apex:inputText value="{!cons.LastName}"/>
</apex:column>
<apex:column >
<apex:commandButton value="EditContact"/>
</apex:column>
</apex:pageblockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class listViewsForContactRecords {
public list<contact> conList{get;set;}
public string recid{get;set;}
set<id> accIds = new set<id>();
public list<account> accountsToDisplay{get;set;}
public account getAcounts() {
return null;
}
public listViewsForContactRecords(){
accountsToDisplay = [select id, name, phone from account limit 10];
}
public void MethodToCall() {
conList = [select id, FirstName, LastName from contact where accountid=:recid];
}
}
I have created test class for controller and page
you will get 100% coverage from this.
Help url:http://www.aboveandbeyondcloud.com/apex-test-classes-best-practices/
Test class
If iformation is informative please choose my answer as best answer.
Thanks,
Varun
All Answers
a) Using a generator of test class for test coverage (because you need to deploy in production very quickly):
Test Class Generator for Salesforce
This app helps you to generate test class for controller, trigger & batch .
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
b) Writing your own class of test for functional tests:
b.1) Find the buttons, links and their actions: here the action is on
<apex:commandLink value="{!acc.name}" action="{!MethodToCall}" rerender="pgbContacts" status="status">
b.2) Find the used data by the public getter/setter: here accounts ( controller ) and contacts ( response to the action )
accountsToDisplay = [select id, name, phone from account limit 10];
conList = [select id, FirstName, LastName from contact where accountid=:recid];
so you need to create a list of account and their contacts.
Utility tool: Salesforce-Test-Factory when you use a lot of columns : https://github.com/dhoechst/Salesforce-Test-Factory
b.3) Find the parameters : none here.
b.4) Salesforce documentation to put all together: it is a custom controller: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
c) Code generated with the Test Class Generator (need the package installed first because of tgen) if you just want the 100% test coverage very quickly :
Best regards
Alain
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
1) Choose Class ( its parser will analyse everything )
2) Build Data : basic but sufficient for your class
3) See your test class
and that's all. I run the result test class: 100% code coverage.
The data are not related between contact and account (basic data generated) but the coverage is 100% nevertheless.
Best regards
Alain
Utility tool: Salesforce-Test-Factory when you use a lot of columns : https://github.com/dhoechst/Salesforce-Test-Factory
Otherwise, you can create a list of account and contacts manually.
Given that the objects are generated by tgen, you can export them in JSON format and reuse them (if you want to delete all the references to tgen).
http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_json.htm
Do you know how to create a list of accounts and contacts manually one by one without a factory (if not I can help you)?
Regards
Alain
tgen.TestData.selectededFields.put('Account',new Set<string>{'Name'});
//TestData.CreateData(ObjectName,Cascade,useAlreadyCreatedObj)
Account account_Obj = (Account)tgen.TestData.CreateData('Account', false,false);
system.debug('account_Obj:' + Json.serialize(account_Obj));
account_Obj = (Account) JSON.deserialize('{"attributes":{"type":"Account"},"Name":"Name737"}',Account.class);
system.debug('name:' + account_Obj.name);
Account account_Obj = (Account)tgen.TestData.CreateData('Account', false,false);
is equivalent to:
Account account_Obj = (Account) JSON.deserialize('{"attributes":{"type":"Account"},"Name":"Name737"}',Account.class);
The sample above it is trivial but with a list with many objects and fields, this technique is the shortest for exporting and creating objects quickly.
Regards
Alain
I have created test class for controller and page
you will get 100% coverage from this.
Help url:http://www.aboveandbeyondcloud.com/apex-test-classes-best-practices/
Test class
If iformation is informative please choose my answer as best answer.
Thanks,
Varun
It is enough according to your controller.
i covered everything .you will get any doubt for test classes i will help you.
When you will deploy it production these will no issue for this class.
spnvarun0121@gmail.com
i will try to solve your problem in the future if you.