+ Start a Discussion
noedskovnoedskov 

FIND vs. SELECT in test method

Hi,

 

I have an Apex Class called AjaxRespController which defines the method doSearch().

 

My test method for the class looks like this:

 

		Community community = [SELECT Id, IsActive FROM Community WHERE IsActive = true LIMIT 1];

Idea idea = new Idea();
idea.Title = 'SNH Test Idea Title';
idea.CommunityId = community.Id;
insert idea;

Test.startTest();
ApexPages.currentPage().getParameters().put('q', 'a');
AjaxRespController arc = new AjaxRespController();
arc.doSearch();
String result = arc.getResult();
System.assertNotEquals(result, null);
Test.stopTest();

In the method doSearch I have the folllowing code:

 

        String searchQ = String.escapeSingleQuotes(params.get('q'));
String query = 'FIND \'*' + searchQ + '*\' IN ALL FIELDS RETURNING Idea(Id, Title, VoteTotal)';
System.debug('Query: ' + query);
Idea myIdea = [SELECT Id, Title, VoteTotal FROM Idea WHERE Title = 'SNH Test Idea Title' LIMIT 1];
if(myIdea != null) {
System.debug('Found Idea: ' + myIdea.Title);
} else {
System.debug('No Idea Found');
}
List<List<SObject>> result = search.query(query);
List<Idea> myIdeas = (List<Idea>)result.get(0);
System.debug('Ideas found: ' + myIdeas.size());

 

Here's what the debug messages tell me:

 

13:16:38.785|USER_DEBUG|[13,3]|DEBUG|Query: FIND '*a*' IN ALL FIELDS RETURNING Idea(Id, Title, VoteTotal)

13:16:38.851|USER_DEBUG|[16,4]|DEBUG|Found Idea: SNH Test Idea Title
13:16:38.852|USER_DEBUG|[22,9]|DEBUG|Ideas found: 0

 

What puzzles me is that the FIND query doesn't find the Idea I insert in my test method but the SELECT statement does. How can that be? What am I missing?

 

All help and ideas are greatly appreciated.

 

Cheers.

 

Søren Nødskov Hansen

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

SOSL queries (aka FIND) don't return anything by default when used in test methods.

 

Instead you have to 'prime' the return value in your test method using Test.setFixedSearchResults.  Search for this in the Apex developer's guide and you'll see an example of using it.

All Answers

bob_buzzardbob_buzzard

SOSL queries (aka FIND) don't return anything by default when used in test methods.

 

Instead you have to 'prime' the return value in your test method using Test.setFixedSearchResults.  Search for this in the Apex developer's guide and you'll see an example of using it.

This was selected as the best answer
noedskovnoedskov

Hi,

 

Great stuff, bob_buzzard. That was exactly what I was missing.

 

Thanks for the heads up!

 

Not sure if it makes a difference, but I changed my test method from what I posted above to the following:

 

 

    static testMethod void testdoSearch() {
    	// Locate Community
		Community community = [SELECT Id, IsActive FROM Community WHERE IsActive = true LIMIT 1];

		// Create Idea
		Idea idea = new Idea();
		idea.Title = 'SNH Test Idea Title';
		idea.CommunityId = community.Id;
		insert idea;

		// Create Fixed Search Results
		Id[] fixedSearchResults = new Id[1];
		fixedSearchResults[0] = idea.Id;

        Test.startTest();

		// Setup PageReference
        PageReference page = ApexPages.currentPage();
        page.getParameters().put('q', 'a');

		// Add PageReference and Fixed Search Results
        Test.setCurrentPage(page);
        Test.setFixedSearchResults(fixedSearchResults);

		// Execute Method
        AjaxRespController arc = new AjaxRespController();
        arc.doSearch();
        String result = arc.getResult();

		// Validate Result
        System.assertNotEquals(result, null);

        Test.stopTest();
    }

 It's a bit more structured now but also I now set the PageReference and Fixed Search Result using the Test keyword. I figured that would be the proper way of doing it compared to how it looked before (see my initial post).

 

 

Again, thanks for the help. Much appreciated.

 

/Søren Nødskov Hansen