Tuesday 18 October 2011

How to Write Good Test Coverage on Visualforce Pages

Once the development phase of your new Visualforce page is finished often many find it a challenge to receive the acceptable code coverage needed to be obtained in order to transport the changes into production instance of Sales force.

Below is a sample of how you can create a test class that will take care of it! This is going to be really interesting...

First i will show you the visual force page and the controller behind it.
What this page does is to simply filter the Account records by their type (picklist) and show them to the user.


Here i write Source Code for Visual force Page and its Controller.

Visual force Page:


<apex:page controller="myPageController" tabStyle="Account">
<apex:form >
<apex:sectionHeader title="My Page" />
<apex:pageBlock title="" id="pageBlock">
<apex:pageBlockButtons location="top">
<apex:inputField id="accountType" value="{!Account.Type}" />
<apex:commandButton value="View" action="{!ViewAccounts}" id="theButton" rerender="pageBlock"></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
<apex:column >
<apex:facet name="header">Account Name</apex:facet>
<apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink>
</apex:column>
<apex:column value="{!a.type}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller :
public class myPageController {
private Account acc = new Account();
private List<Account> accs;
public Account getAccount()
{
return acc;
}
public void setAccount(Account a)
{
acc = a;
}
public List<Account> getAccounts()
{
return accs;
}
public PageReference ViewAccounts()
{
if (acc.Type == null)
{
//view a message because validation has failed.
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Validation Failed: Select an item fron the dropdown list.'));
return null;
}
string AccountType = acc.Type;
accs = [Select id, name, type from Account where type= :AccountType limit 20];
if (accs == null || accs.size() == 0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No data found to be shown.'));
}
return null;
}
}



Now it is the moment! The most important part: the tester class.
In order to get a the best results following things should be included in your test method:
  • Create a reference to your page and set the Test object's current reference to your page (shown below)
  • Create a new instance of your controller (if you are using a Custom Controller or Extension)
  • Consider all possible scenarios (Test Cases) and try calling all properties and methods of your controller (Demonstrated below)
Test Class :

public class AccountCheck {
static testmethod void Account()
{
//Test converage for the myPage visualforce page
  PageReference pageref = Page.AccountCheckbox;
  Test.setCurrentPageReference(pageref);
// create an instance of the controller

  AccountCheckbox acc = new AccountCheckbox();
   //try calling methods/properties of the controller in all possible scenarios

// to get the best coverage
List<Account> selectedAccounts = new List<Account>();
  acc.getSelected();
  acc.GetSelectedAccounts();
  acc.getAccounts();
     }
    }


No comments:

Post a Comment