+ Start a Discussion

SOSL query not returning values when used in testMethods

Here is the code but it appears the SOSL query is not returning the account that was just inserted:

public class soslTest {
 public static testMethod void SOSLtest(){
  Account acct = new Account(
   Name = 'Test Account'
  insert acct;
  List<List<SObject>> searchList = [FIND '*Test Account*' IN NAME FIELDS RETURNING Account(Id, name)]; 
  Account [] accounts = ((List<Account>)searchList[0]);
  system.debug('Accounts ' + accounts);

//And the debug line:

20080610183243.419:Class.soslTest.SOSLtest: line 12, column 9: Accounts ()

 Any ideas?

SOSL is a special case.  Check the latest Apex Language Reference PDF for more info on SOSL in testMethods.  There is a section (currently on page 124) in the PDF called "Adding SOSL Queries to Unit Tests".

To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that
is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the
query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of
record IDs that are returned by the search...


Thanks for pointing that out. When this document first came out it was around 100 pages but as it starts to push beyond 300 pages it's getting harder to take it all in.