SOSL search in a test with no fixed search results

ID

apex.correctness.sosl_fixed_search_results

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Test Quality

Language

Apex

Description

Reports a SOSL [FIND …​] inside an @IsTest method whose body never calls Test.setFixedSearchResults.

Rationale

The search index is not populated inside a test context, so a SOSL query run there returns an empty result for every record the test just inserted. The test does not fail on that: it asserts over an empty list, the size check passes because nothing was expected either, and the case reports success without ever exercising the search. The consequence is a search feature that carries full coverage and has never been run against a single matching record, so a broken RETURNING clause or a wrong field list reaches production untested.

The following code illustrates the pattern detected by this rule:

@IsTest
private class AccountSearchTest {

    @TestSetup
    static void setup() {
        insert new Account(Name = 'Acme Corporation');
    }

    @IsTest
    static void searchFindsTheAccount() {
        // FLAGGED: SOSL search in a test with no fixed search results
        List<List<SObject>> results = [FIND 'Acme' IN ALL FIELDS RETURNING Account(Id, Name)];
        Assert.areEqual(1, results[0].size(), 'the setup account was expected');
    }

    @isTest
    static void searchIsCaseInsensitive() {
        // FLAGGED: SOSL search in a test with no fixed search results
        List<List<SObject>> results = [FIND 'acme' IN NAME FIELDS RETURNING Account(Id)];
        Assert.areEqual(1, results[0].size(), 'the search should ignore case');
    }

    @IsTest
    static void searchFindsTheFixedAccount() {
        Account a = [SELECT Id FROM Account];
        Test.setFixedSearchResults(new List<Id>{ a.Id });
        List<List<SObject>> results = [FIND 'Acme' IN ALL FIELDS RETURNING Account(Id, Name)];
        Assert.areEqual(1, results[0].size(), 'the fixed result was expected');
    }

    @IsTest
    static void queryFindsTheAccount() {
        List<Account> found = [SELECT Id FROM Account];
        Assert.areEqual(1, found.size(), 'SOQL is unaffected by the search index');
    }
}

Remediation

Call Test.setFixedSearchResults(new List<Id>{ …​ }) with the ids the case inserted, before the SOSL query runs; the query then returns exactly those records, filtered by the RETURNING clause. Assert on the returned records, not only on the list size.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved