5

Following the example laid out in Adding SOSL Queries to Unit Tests, I created a test method that inserts a ContentNote record, sets the results using Test.setFixedSearchResults(), and then performs a SOSL query.

@isTest 
static void sampleTestMethod(){
    ContentNote n = New ContentNote(
        Title='test',
        Content = Blob.valueof('test')
    );

    insert n;

    Id [] fixedSearchResults= new Id[1];
    fixedSearchResults[0] = n.Id;
    Test.setFixedSearchResults(fixedSearchResults);
    List<List<SObject>> searchList = [FIND 'test' 
                                      IN ALL FIELDS RETURNING 
                                      ContentNote(id, title)];

    system.assertEquals(1,searchList[0].size());
}

I expected that there would be one result record, however, the system.assertEquals fails, indicating that no results were returned.

This pattern for performing SOSL queries in test methods works as expected with other objects, but not with ContentNote.

In order to reproduce this enhanced notes must be enabled in your org.

8
  • +1 for a simple MVR. Have you tried setting SeeAllData=true?
    – Adrian Larson
    Commented Jan 26, 2017 at 3:41
  • SOSL performs 'text' search, whereas Content is stored as blob, though SOSL is not searching ContentNote Commented Jan 26, 2017 at 5:28
  • normally the SOSL query is in the code being tested, not in the testmethod. Does that make a difference?
    – cropredy
    Commented Jan 26, 2017 at 5:39
  • @cropredy your point is valid, I have also checked that, through SOQL query in test class it returns the record and content is returning as Blob Commented Jan 26, 2017 at 5:53
  • I faced similar situation once and found that sosl queries don't run in test context. You need to manually populate your list as if it was returned from sosl query.
    – sf_user
    Commented Jan 26, 2017 at 8:44

1 Answer 1

2

SOSL queries are tested similar to callouts using something like a Mock called fixedSearchResults(). See Adding SOSL Queries to Unit Tests in the Apex Docs. Here's an example from the docs on one way it can be used:

@isTest
private class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = '001x0000003G89h';
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'test' 
                                         IN ALL FIELDS RETURNING 
                                            Account(id, name WHERE name = 'test' LIMIT 1)];
    }
}

Here's what you'll find in the Test Class docs on setFixedSearchResults(setSearchResults).

setFixedSearchResults(setSearchResults)

Defines a list of fixed search results to be returned by all subsequent SOSL statements in a test method. Signature

public static Void setFixedSearchResults(ID[] setSearchResults)

Parameters

setSearchResults Type: ID[] The list of record IDs specified by opt_set_search_results replaces the results that would normally be returned by the SOSL queries if they were not subject to any WHERE or LIMIT clauses. If these clauses exist in the SOSL queries, they are applied to the list of fixed search results. Return Value

Type: Void

Usage

If opt_set_search_results is not specified, all subsequent SOSL queries return no results.

For more information, see Adding SOSL Queries to Unit Tests.

EDIT:

Having looked at the Object Reference for Content Note, I suspect your issue may be in how you're creating the record in your test method. If you create the record as follows, I suspect your issue will disappear.

ContentNote n = new ContentNote();
n.Title = 'test';
String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
n.Content = Blob.valueOf(body.escapeHTML4());
insert(n);

Alternatively, it could be created like this:

ContentNote n = new ContentNote();
n.Title = 'test';
String body = '<b>Hello World. Because this text is already formatted as HTML, it does not need to be escaped. 
Special characters such as &quot;, etc. must already use their HTML equivalents.</b>';
n.Content = body;
insert(n);

If you add a system.debug('content record id = ' + n.Id ) to your test class, you may discover that you're not getting the record inserted properly as you might think.

5
  • I am familiar with the document you linked to, and I am using the fixedSearchResults() method to set the results. Despite this, no results are returned when the test method executes the SOSL query.
    – bardsley
    Commented Jan 26, 2017 at 23:45
  • See my edited answer
    – crmprogdev
    Commented Jan 27, 2017 at 0:10
  • I also didnt find the results from SOSL Commented Jan 27, 2017 at 0:10
  • @crmprogdev I tried the alternative methods you suggested for creating the ContentNote and the issue persisted. I also tried adding to my original sample code the debug statement as you suggested. The result was USER_DEBUG|[154]|DEBUG|content record id = 06941000000k7adAAA which seems to indicate that the ContentNote is being successfully inserted...
    – bardsley
    Commented Jan 27, 2017 at 3:18
  • 1
    I have verified the same results. I've attempted several different things without success and am making some further inquiries. Will post an update should I learn anything helpful.
    – crmprogdev
    Commented Jan 27, 2017 at 20:16

You must log in to answer this question.

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