You need to sign in to do that
Don't have an account?
CRNR
Test Method Coverage for Dynamic Search Controller
I am new to test writing test methods and was hoping I could figure this one out on my own, but at some point I have to give in! I am getting 48% coverage on this controller. It is based on the dynamic search page provided by Jeff Douglas (http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/). For now I have place the test method in the controller. Here is the controller with test method:
public with sharing class ProductSearchController { // the soql without the order and limit private String soql {get;set;} // the collection of products to display public List<NRProducts__c> products {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 NRCode 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 200'; } set; } // init the controller and display some sample data when the page loads public ProductSearchController() { soql = 'select Name, FBA_Total_Value__c, Total_Qty_Purchased__c, Create_PO__c, isFBA__c, Vendors__c, VendorID__c, Total_Sold__c, FBA_Inventory__c, Open_Qty__c, Total_Qty__c, Inventory_Burn__c, FBA_Turn__c, Reorder_Point__c, Reorder__c, Reorder_Amount__c, Reorder_Cost__c from NRProducts__c where isFBA__c = True'; 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 { products = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'); } catch (Exception e) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!')); } } // runs the search with parameters passed via Javascript public PageReference runSearch() { String name = Apexpages.currentPage().getParameters().get('name'); String lastSold = Apexpages.currentPage().getParameters().get('lastSold'); String vendors_id = Apexpages.currentPage().getParameters().get('vendors_id'); String fbaInventory = Apexpages.currentPage().getParameters().get('fbaInventory'); String openQty = Apexpages.currentPage().getParameters().get('openQty'); String totalQty = Apexpages.currentPage().getParameters().get('totalQty'); String inventoryBurn = Apexpages.currentPage().getParameters().get('inventoryBurn'); String fbaTurn = Apexpages.currentPage().getParameters().get('fbaTurn'); String reorderPoint = Apexpages.currentPage().getParameters().get('reorderPoint'); String reorder = Apexpages.currentPage().getParameters().get('reorder'); String reorderAmount = Apexpages.currentPage().getParameters().get('reorderAmount'); String reorderCost = Apexpages.currentPage().getParameters().get('reorderCost'); soql = 'select Name, isFBA__c, Total_Qty_Purchased__c, FBA_Total_Value__c, Create_PO__c, Vendors__c, VendorID__c, Total_Sold__c, FBA_Inventory__c, Open_Qty__c, Total_Qty__c, Inventory_Burn__c, FBA_Turn__c, Reorder_Point__c, Reorder__c, Reorder_Amount__c, Reorder_Cost__c from NRProducts__c where isFBA__c = True'; if (!name.equals('')) soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\''; if (!vendors_id.equals('')) soql += ' and VendorID__c LIKE \''+String.escapeSingleQuotes(vendors_id)+'%\''; if (!lastSold.equals('')) soql += ' and Total_Sold__c >= '+lastSold+''; if (!fbaInventory.equals('')) soql += ' and FBA_Inventory__c >= '+fbaInventory+''; if (!openQty.equals('')) soql += ' and Open_Qty__c >= '+openQty+''; if (!totalQty.equals('')) soql += ' and Total_Qty__c >= '+totalQty+''; if (!inventoryBurn.equals('')) soql += ' and Inventory_Burn__c >= '+inventoryBurn+''; if (!fbaTurn.equals('')) soql += ' and FBA_Turn__c >= '+fbaTurn+''; if (!reorderPoint.equals('')) soql += ' and Reorder_Point__c >= '+reorderPoint+''; if (!reorder.equals('')) soql += ' and Reorder__c LIKE \''+String.escapeSingleQuotes(reorder)+'%\''; if (!reorderAmount.equals('')) soql += ' and Reorder_Amount__c >= '+reorderAmount+''; if (!reorderCost.equals('')) soql += ' and Reorder_Cost__c >= '+reorderCost+''; // run the query again runQuery(); return null; } /////////////////////////////////////////////// // // Unit Tests // /////////////////////////////////////////////// public static testMethod void testController() { // instantiate the controller ProductSearchController ctrl=new ProductSearchController(); // instantiate the controller //ctrl.runSearch(); System.assert(ctrl.runSearch() == null); } }
I have color coded based on what the testing shows. Blue = coverage, Red = no coverage.
Thank you for your help!
Before you instantiate the controller, you need to instantiate a web page and put parameters into the URL. Note you have to use the getParameters method to do this. Then you can instantiate and execute the controller method.
CRNR
Did the posted answer work for you ? I've been struggling with the same issue with no luck. I saw this post and answer and got excited.
I basically have the same code as you do (with a different controller name and different fields) and when I try to save the following test file I get an error "Method does not exist or incorrect signature: ctrl.runSearch()"
The test file is:
public class test_VisualForcePage {
static testMethod void myUnitTest() {
//Use the PageReference Apex class to instantiate a page
PageReference pageRef = Page.Direct_Account_Page;
//In this case, the Visualforce page named ‘Direct_Account_Page’ is the starting point of this test method.
Test.setCurrentPageReference(pageRef);
//Instantiate and construct the controller class.
pageref.getParameters().put('AccountNumber','10-101');
pageref.getParameters().put('Product','SMA');
pageref.getParameters().put('OganizationName','acme');
DirectAccountController MyPageCon = new DirectAccountController();
System.assert(ctrl.runSearch() == null);
}
}
The controllwer is:
public with sharing class DirectAccountController {
// the soql without the order and limit
private String soql {get;set;}
// the collection of DirectAccounts to display
public List<Direct_Account__c> DirectAccounts {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 accountNumber
public String sortField {
get { if (sortField == null) {sortField = 'AccountNumber'; } 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 DirectAccountController() {
/* soql = 'select firstname, lastname, account.name from Direct_Account__c where */
soql = 'select Account_Number__c, Product__c , Organization_Name__c from Direct_Account__C where account.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 {
DirectAccounts = 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 AccountNumber = Apexpages.currentPage().getParameters().get('AccountNumber');
String Product = Apexpages.currentPage().getParameters().get('Product');
String OrganizationName = Apexpages.currentPage().getParameters().get('OrganizationName');
/* String technology = Apexpages.currentPage().getParameters().get('technology'); */
soql = 'select Account_Number__c, Product__c, Organization_Name__c from Direct_Account__c where Name != null';
if (!AccountNumber.equals(''))
soql += ' and Account_Number__c LIKE \''+String.escapeSingleQuotes(AccountNumber)+'%\'';
if (!Product.equals(''))
soql += ' and Product__c LIKE \''+String.escapeSingleQuotes(Product)+'%\'';
if (!OrganizationName.equals(''))
soql += ' and Organization_Name__c LIKE \''+String.escapeSingleQuotes(OrganizationName)+'%\'';
/* if (!technology.equals(''))
soql += ' and interested_technologies__c includes (\''+technology+'\')'; */
// run the query again
runQuery();
return null;
}
}
Any ideas?
Sorry, a typo in the test method:
I showed: System.assert(ctrl.runSearch() == null);
It is actually: System.assert(MyPageCon.runSearch() == null);
I get an error: Method does not exist or incorrect signature: [DirectAccountController].runSearch()
My controller has the runSearch method just as is shown in this thread's otehr code example.
I am frustrated that I can't figure this out. I hope someone out there will have an idea.
Thanks in advance
For one, change
to
Anyone who has solved this?