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. All SOSL queries that take place later in the test method return the list of record IDs that were specified by the Test.setFixedSearchResults method. Additionally, the test method can call Test.setFixedSearchResults multiple times to define different result sets for different SOSL queries. If you do not call the Test.setFixedSearchResults method in a test method, or if you call this method without specifying a list of record IDs, any SOSL queries that take place later in the test method return an empty list of results.

The list of record IDs specified by the Test.setFixedSearchResults method replaces the results that would normally be returned by the SOSL query if it were not subject to any WHERE or LIMIT clauses. If these clauses exist in the SOSL query, they are applied to the list of fixed search results.

Note:
1. Although the record may not match the query string in the FIND clause, the record is passed into the RETURNING clause of the SOSL statement.
2. If the record matches the WHERE clause filter, the record is returned. If it does not match the WHERE clause, no record is returned.

Sample code:

Apex Controller:

  1. public class SOSLController {  
  2.   
  3.     public static List < List < SObject > > searchAccountContactLead( String strSearch ) {  
  4.       
  5.         String searchQuery = 'FIND \'' + strSearch + '*\' IN ALL FIELDS RETURNING Account( Id, Name WHERE Industry = \'Apparel\' ), Contact, Lead';   
  6.         return search.query( searchQuery );  
  7.       
  8.     }  
  9.       
  10. }  

Test class:

  1. @isTest  
  2. private class SOSLControllerTest {  
  3.   
  4.     static testMethod void testSOSL() {  
  5.       
  6.         Account acc = new Account( Name = 'Testing', Industry = 'Banking' );  
  7.         insert acc;  
  8.         List < List < SObject > > searchResults = SOSLController.searchAccountContactLead( 'Sample' );  
  9.         List < Account > listAccount = searchResults.get( 0 );  
  10.         system.assertEquals( 0, listAccount.size() );//Size will be zero since setFixedSearchResults is not set  
  11.         Id [] fixedSearchResults = new Id[1];  
  12.         fixedSearchResults[0] = acc.Id;  
  13.         Test.setFixedSearchResults( fixedSearchResults );  
  14.         searchResults = SOSLController.searchAccountContactLead( 'Sample' );  
  15.         listAccount = searchResults.get( 0 );  
  16.         system.assertEquals( 0, ( (List<Account>)searchResults.get( 0 ) ).size() );//Size will be still zero since WHERE condition fails for account  
  17.         acc.Industry = 'Apparel';  
  18.         update acc;  
  19.         searchResults = SOSLController.searchAccountContactLead( 'Sample' );  
  20.         listAccount = searchResults.get( 0 );  
  21.         system.assertEquals( 1, listAccount.size() );//Size will be one since Account WHERE conditions succeeds  
  22.       
  23.     }  
  24.       
  25. }  

No comments:

Post a Comment