1

I have a contact with name Test Contact in the Org.

I run the SOSL like below in Query Editor and able to get results of contacts containing Test in their name:

Find {*Test*} IN ALL FIELDS RETURNING Contact(Id,Name,Email)

I am working on a test class.

In the test class, I have created a contact with FirstName as Test and LastName as Contact.

I have tried to run SOSL as below ways but not able to get any result:

1.

List<List<sObject>> searchResults = [Find '{*Test*}' IN ALL FIELDS RETURNING Contact(Id,Name,Email)];

2.

String searchTextWithWildCards = '*Test*';
List<List<sObject>> searchResults = [Find :searchTextWithWildCards  IN ALL FIELDS RETURNING Contact(Id,Name,Email)];

3.

String searchTextWithWildCards = '%Test%';
List<List<sObject>> searchResults = [Find :searchTextWithWildCards  IN ALL FIELDS RETURNING Contact(Id,Name,Email)];

What am I missing here?

3

Refer the documentation 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 test method code will be like this and it works at my DE.

@isTest
private class TestSOSLQuery {

    public static testMethod void testSoslFixedResults() {
    
       Account acct = new Account(Name='testAccount');
       insert acct;
       
       Contact con = new Contact(FirstName='Test', LastName='Contact', AccountId= acct.id);
       insert con;
    
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = con.Id;
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'Test%' 
                                         IN ALL FIELDS RETURNING 
                                          Contact(Id,Name,Email)];
       System.debug('searchList=' + searchList);
    }
}

It returns as follows:

USER_DEBUG [18]|DEBUG|searchList=((Contact:{Id=0030o00003CJcudAAD, Name=Test Contact}))

| improve this answer | |
  • The SOSL is being executed in method under test actually. If I fix the search results, then how would I know that the SOSL written in the method under test is correct or not? – वरुण Jan 15 at 6:56
  • 2
    @वरुण If your search query is invalid, you will get a QueryException and the test will fail (unless the exception is caught). You cannot validate the "proper" results are returned in a unit test. Test manually and come up with your own conclusions. – sfdcfox Jan 15 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.