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
mmaxtrammaxtra 

Help with testmethod please???

Hi I am trying to get this testmethod to work for my SOSL and I can't figure out what am I doing wrong?I get 25% with an error...

public class SimpleStepSearchController { public PageReference Start() { PageReference SimpleStep = Page.SimpleStep1; SimpleStep.setRedirect(true); return SimpleStep; } String searchText; List<Account> results; List<Contact> resultsc; public String getSearchText(){ return searchText; } public void setSearchText(String s) { searchText = s.endsWith('*') ? s : s + '*'; //searchText = s; } public List<Account> getResults() { return results; } public List<Contact> getResultsc() { return resultsc; } public PageReference doSearch() { results = (List<Account>)[FIND :searchText IN NAME FIELDS RETURNING Account(Name,BillingState,BillingCity,Phone,Website,Id,RecordType.Name)] [0]; resultsc = (List<Contact>)[FIND :searchText IN NAME FIELDS RETURNING Contact(Name,Account.Name,Id,AccountId,Phone,Email,RecordType.Name)] [0]; return null; } public static testMethod void testSimpleStepSearchController() { //Create a reference to the VF Page being tested, this is needed if you are passing parameters in the URL //or if you are going to have actions on the page that direct to a different page. PageReference pageRef = Page.SimpleStepSearchPage; Test.setCurrentPage(pageRef); //Instantiate the controller SimpleStepSearchController controller = new SimpleStepSearchController(); String searchText; searchText= 'fa'; //searchText=controller.getItems(); //searchText=controller.getitems(); //push the button on the page String searchquery = 'FIND :searchText IN NAME FIELDS RETURNING Contact(Name,Account.Name,Id,AccountId,Phone,Email,RecordType.Name)'; List<List<SObject>> junkSearch = search.query(searchquery); controller.doSearch(); PageReference pageRef1 = Page.SimpleStep1; Test.setCurrentPage(pageRef1); } }

 


bob_buzzardbob_buzzard

Unit tests involving SOSL queries are a bit different to usual - if you execute the SOSL query it will return an empty list of results, unless you set the results that will be returned when your query executes using the Test.setFixedSearchResults() method - see page 117 of the Apex Developer's Guide for more information.

 

Can you post the error that you are getting - it sounds like your test might be failing part way through. 

bob_buzzardbob_buzzard
As far as I can tell, you aren't setting searchtext into your controller, so the doSearch will bind in a null variable.
MMA_FORCEMMA_FORCE

You are absolutely correct..My error is:

System.NullPointerException: Attempt to de-reference a null objectClass.SimpleStepSearchController.doSearch: line 30, column 41 Class.SimpleStepSearchController.testSimpleStepSearchController: line 57, column 6 External entry point

 

But I thought I was binding the searchtext in my SOSL??? and the dosearch should work no??

bob_buzzardbob_buzzard

You are binding the controller property, but you haven't assigned anything to it at the time that the search runs.

 

After you set your local variable searchtext to 'fa', you need to call controller.setSearchText to set the value used by the controller.

bob_buzzardbob_buzzard

Post/mark the lines that haven't got coverage and I'll endeavour to help.

 

MMA_FORCEMMA_FORCE

Got it to 100%...

 Can you tell me if I have any issues with this testmethod... Should be ok if it got to 100% right??

 

public class SimpleStepSearchController { //Variables Section String searchText; List<Account> results; List<Contact> resultsc; /////////////////////////////////////////////////////////////////////////////////////////////////////// public String getSearchText(){ return this.searchText; } public void setSearchText(String searchText) { this.searchText = searchText.endsWith('*') ? searchText: searchText + '*'; } public List<Account> getResults() { return this.results; } public List<Contact> getResultsc() { return this.resultsc; } public PageReference doSearch() { results = (List<Account>)[FIND :searchText IN NAME FIELDS RETURNING Account(Name,BillingState,BillingCity,Phone,Website,Id,RecordType.Name)] [0]; resultsc = (List<Contact>)[FIND :searchText IN NAME FIELDS RETURNING Contact(Name,Account.Name,Id,AccountId,Phone,Email,RecordType.Name)] [0]; return null; } public PageReference Start() { PageReference SimpleStep = Page.SimpleStep1; SimpleStep.setRedirect(true); return SimpleStep; } public static testMethod void testSimpleStepSearchController() { //Instantiate the controller SimpleStepSearchController controller = new SimpleStepSearchController(); controller.setSearchText('far'); string ss = controller.getSearchText(); List<Contact> l1 = controller.getResultsc(); List<Account> l12 = controller.getResults(); controller.doSearch(); controller.Start(); } }

 

 

 

bob_buzzardbob_buzzard
There is coverage, but its not actually verifying anything - e.g. there are no asserts that what is being returned by the various controller methods is as expected.  
MMA_FORCEMMA_FORCE

Would you happen to have a Assert Method for Search or that relates back to my search criteria?

Please...

Thanks

bob_buzzardbob_buzzard

As I said earlier on, SOSL testing is a bit different in that you have to register the results you expect or always return an empty list.  I usually register the results and then assert that the controller field(s) handling the search results matches.

 

I also assert that returned PageReferences are as I expect them - that way if someone changes the page flow unintentionally, it will get picked up.