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
BrookesyBrookesy 

Test Class with PageReference

Iv been having some issues getting complete coverage on the piece of code below. Its a simple search feature so people can search for limited information across regions (to assist in sales ops in other countries) You can do a basic search or a loose search.

 

public class CustomerSearchController { String acctName =''; Boolean looseSearch; List <Account> accountsList = new List <Account>(); public Void setacctName(String input) { acctName = input; } public Void setlooseSearch(Boolean input) { looseSearch = input; } public List <Account> getaccountsList() { return accountsList; } public String getacctName() { return null; } public Boolean getlooseSearch() { return null; } public PageReference Search() { accountsList =new List<account>(); if (looseSearch) { for( List <Account> temp : ([select id, name, Owner.Name, Sales_Stage__c, Branch__c from Account where name like: '%'+acctName+'%'])) { accountsList.addAll(temp); } }

else { for( List <Account> temp : ([select id, name, Owner.Name, Sales_Stage__c, Branch__c from Account where name like: acctName+'%'])) { accountsList.addAll(temp); } } return null; } }

 

My test class cant manage to get inside the public PageReference Sarch() method. And i was wondering how i do this? The test class is pretty simple as you can see, but only covers 61% of the code. Everything below the pageref method isnt tested
 
My question is how can i replicate the pagereference information and  fix my test class so that it will cover this code?
 
Any help would be great! 

 

 

@isTest private class CustomerSearchControllerTestMethod { static testMethod void searchControllerTest() { String input = 'Account'; String acctName = input; Boolean looseSearch = True; CustomerSearchController controller = new CustomerSearchController(); controller = new CustomerSearchController(); controller.setacctName('Account'); controller.setlooseSearch(true); controller.getaccountsList(); controller.getacctName(); controller.getlooseSearch(); List <Account> accountsList = new List <Account>(); accountsList = new List<account>(); } }

 

Message Edited by Brookesy on 08-17-2009 06:59 AM
Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

You can execute the search like a void method

 

controller.search();

 

will execute the search method.

You should also then check to make sure the values in your properties got changed as you expected.

I would also recommend that you change your loosesearch boolean and run the search again, just to cover both conditions of your "if" logic in the search.

 

 

All Answers

JimRaeJimRae

You can execute the search like a void method

 

controller.search();

 

will execute the search method.

You should also then check to make sure the values in your properties got changed as you expected.

I would also recommend that you change your loosesearch boolean and run the search again, just to cover both conditions of your "if" logic in the search.

 

 

This was selected as the best answer
Brookesy--Brookesy--

Thanks for the quick response Jim!

 

I executed it like a void like you said that that cleared out that search function. Now at 85%.

 

Il write up the rest of your recommendations and should be at 100% no probs!!

 

Thank you :)

 

Michael