I have EditorModalController.searchDoctors that returns a list of contacts.

public class EditorModalController {

    public static List<Contact> searchDoctors(String searchValue) {
        String s = '*' + searchValue + '*';
        String doctorRecordTypeId = Schema.Sobjecttype.Contact.getRecordTypeInfosByName().get('Doctor').getRecordTypeId();

        List<List<SObject>> searchList = [FIND :s IN ALL Fields
                RETURNING Contact (Id, FirstName, LastName, NPI__c, MailingCity, MailingState WHERE RecordTypeId =: doctorRecordTypeId)];

        return searchList[0];

This function works great in my lightning component. I've tried to apply test coverage

private class EditorModalControllerTest {

    @testSetup private static void setup() {
        String doctorRecordTypeId = Schema.Sobjecttype.Contact.getRecordTypeInfosByName().get('Doctor').getRecordTypeId();
        insert new Contact(LastName = 'MCDANIEL', RecordTypeId = doctorRecordTypeId);

    @isTest private static void searchDoctors() {
        System.assertEquals(1, [SELECT count() FROM Contact WHERE LastName = 'MCDANIEL']);
        System.assertEquals(1, EditorModalController.searchDoctors('MCDANIEL').size()); 

The first assert passes, but the second does not. I don't understand why that is.

2 Answers 2


SOSL doesn't return results in test classes, so you have to use special methods to set the results.

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.


   Id [] fixedSearchResults= new Id[1];
   fixedSearchResults[0] = '001x0000003G89h';
   List<List<SObject>> searchList = [FIND 'test' 
                                     IN ALL FIELDS RETURNING 
                                        Account(id, name WHERE name = 'test' LIMIT 1)];

See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_SOSL.htm


This is "by design" apparently. See the Salesforce documentation for the reason and how to simulate results using Test.setFixedSearchResults.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .