Why is SOSL in Test not working?

I have a Custom Object that contains a long text area with 32.000 chars full of comma-separated Ids. I need to find all such records that contain one or more ids.

I tried to implement this in this test:

private static void soslWorksAsExpected() {
    // Setup
    List<Id> ids = insertOppsWithAmountZeroAndReturnIds(10);
    SObjectWork__c pendingWork = new SObjectWork__c( 
                                    ltxt_SObjectIds__c = '' + ids.get(2) + ',' + ids.get(7),
                                    txt_ProcessorClassName__c = 'SObjectWorkTestHelper.ExampleSObjectProcessor',
                                    pkl_CurrentState__c = 'QUEUED');
    insert pendingWork;

    List<Id> pendingWorkIds = new List<Id>{pendingWork.Id};
    System.debug('# pendingWorkIds' + pendingWorkIds);

    // Execute
    String orPhrase = String.join(pendingWorkIds, ' OR ');
    System.debug('# orPhrase' + orPhrase);
    List<SObject> soqlResults = [SELECT Id, ltxt_SObjectIds__c FROM SObjectWork__c WHERE pkl_CurrentState__c = 'QUEUED'];
    System.debug('# soqlResults' + soqlResults);
    List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ALL FIELDS RETURNING SObjectWork__c(Id, ltxt_SObjectIds__c)];
    System.debug('# soslResults' + soslResults);

    // Verify
    System.assert( !soslResults.isEmpty() );    
    System.assert( !soslResults.get(0).isEmpty() );     

but it always fails. As I can see from the debug statements everything works. Except the


does not change anything.

What am I doing wrong?

I checked if I can search for this record type in the Search and it worked.


You didn’t mention it was an unit test context 🙂

Testing SOSL is somewhat messy because of indexing delay. You might have to do manual tests of your functionality first and get back to the unit test once you’re happy with it. Then in actual code play with Test.setFixedSearchResults().

Check http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm for more info and example?

Source : Link , Question Author : Robert Sösemann , Answer Author : eyescream

Leave a Comment