You need to sign in to do that
Don't have an account?
can any one help me out with testclass for visualforce custom controller class
Hi guys. I am learning force.com and currently i am using developer account so I necessarily don't have to write testclass for a now but when i will be on project i have to write down test method to deployee it to production envirionment from developer environment. I have written some test classes for triggers but i don't know how to write testmethod for visualforce custome controller class. my code is successfully working but I also want to write test class with cover more than 75%. i am pasting my code bellow can you guys help me out with its test class.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public with sharing class searchPatientAccount {
// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<Account> accounts {get;set;}
// the current sort direction. defaults to asc
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'name'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
// init the controller and display some sample data when the page loads
public searchPatientAccount() {
soql = 'select a.id, a.name, a.accountnumber,a.phone from account a where a.name != null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
// runs the actual query
public void runQuery() {
try {
accounts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
}
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
String accountname = Apexpages.currentPage().getParameters().get('accountname');
String accountnumber = Apexpages.currentPage().getParameters().get('accountnumber');
String phone = Apexpages.currentPage().getParameters().get('phone');
// String technology = Apexpages.currentPage().getParameters().get('technology');
soql = 'select a.id, a.name, a.accountnumber,a.phone from account a where a.name != null';
if (!accountname.equals(''))
soql += ' and a.name LIKE \''+String.escapeSingleQuotes(accountname)+'%\'';
if (!accountnumber.equals(''))
soql += ' and a.accountnumber LIKE \''+String.escapeSingleQuotes(accountnumber)+'%\'';
if (!phone.equals(''))
soql += ' and a.phone LIKE \''+String.escapeSingleQuotes(phone)+'%\'';
// run the query again
runQuery();
return null;
}
// use apex describe to build the picklist values
public List<String> technologies {
get {
if (technologies == null) {
technologies = new List<String>();
Schema.DescribeFieldResult field = Contact.interested_technologies__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
technologies.add(f.getLabel());
}
return technologies;
}
set;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//First you want to set up all your dummy data
//Insert Dummy Accounts here
//Then you want to set your test to your VF page, and instatiate your controller
PageReference pg = Page.YOUR _PAGE_NAME;
Test.setCurrentPage(pg);
searchPatientAccount controller = new searchPatientAccount ();
//Insert your parameters that the page would pass to the controller
System.currentPagereference().getParameters().put('accountname','DUMMY ACCOUNT NAME');
System.currentPagereference().getParameters().put('accountnumber','DUMMY ACCOUNT NUMBER');
System.currentPagereference().getParameters().put('phone','555-555-1212');
System.currentPagereference().getParameters().put('technology','DUMMY ACCOUNT TECHNOLOGY');
//Then you really just want to set your properties, and run your methods and getters
controller.sortDir = 'asc';
controller.sortField = 'name';
controller.searchPatientAccount;
controller.toggleSort;
List<String> testTechs = controller.technologies;
/*That should get you close. You are going to have to set some different values for some of your proerties in order to get coverage on all your IF statements, but you get the idea. Your also going to want to do some system.assertequals statements to make sure your code is accomplishing your desired results, but this will get you the coverage you are looking for, or at least very close, you will just have to change the controller properties to account for all your conditionals in your methods */
Hope this helps
Chris