6

Test Class

    static testMethod void testBehavior() {
        User objUser = [SELECT Id FROM User LIMIT 1];
        System.runAs(objUser) {
            String strContactId = String.valueOf([SELECT Id FROM Contact LIMIT 1].Id);
            Knowledge__kav objKav = new Knowledge__kav(Title = 'TestTitle', UrlName = 'TestTitle');
            insert objKav;
            knowledge__kav objKav2 = [SELECT Id,Title,KnowledgeArticleId FROM knowledge__kav WHERE Id =: objKav.Id];
            KbManagement.PublishingService.publishArticle(objKav2.KnowledgeArticleId, true);
            Test.startTest();
            CaseCreationLightingController_ACE.createCase(strContactId, 'New', 'Phone', 'TestTitle');
            CaseCreationLightingController_ACE.fetchKnowledgeArticles(new List<String>{
                    'TestTitle'
            });
            Test.stopTest();
            Case objCase = [SELECT Id, Status, Origin FROM Case LIMIT 1];
            CaseComment objCaseComment = [SELECT Id, CommentBody, ParentId FROM CaseComment LIMIT 1];
            System.assertEquals(objCase.Id, objCaseComment.ParentId);
            System.assertEquals('New', objCase.Status);
            System.assertEquals('Phone', objCase.Origin);
        }
    }

Method called

    public static List<KnowledgeArticleWrapper_ACE> fetchKnowledgeArticles(List<String> lstComments) {
        List<KnowledgeArticleWrapper_ACE> lstArticleWrapper = new List<KnowledgeArticleWrapper_ACE>();
        List<String> lstErrors = new List<String>();
        // Generating a dynamic "OR" string which needs to be passed as a search parameter to the SOSL
        try {
            String strSearchTerms = '';
            for (String strComment : lstComments) {
                if (!String.isEmpty(strComment)) {
                    strSearchTerms += '("' + strComment + '")';
                }
                if (!String.isEmpty(strComment)) {
                    if (!String.isEmpty(strSearchTerms)) {
                        strSearchTerms += ' OR ';
                    }
                }
            }
            // Doing this to remove the trailing OR.
            if (strSearchTerms.endsWith(' OR ')) {
                strSearchTerms = strSearchTerms.removeEnd(' OR ');
            }
            Search.SearchResults objSearchResults = null;
            List<Search.SearchResult> lstSearchResults = new List<Search.SearchResult>();
            if (!String.isEmpty(strSearchTerms)) {
                objSearchResults = Search.find('FIND :strSearchTerms IN ALL FIELDS RETURNING KnowledgeArticleVersion(Id, Title, UrlName' +
                        ' WHERE PublishStatus = \'Online\' ORDER BY CreatedDate DESC LIMIT 3)');
                lstSearchResults = objSearchResults.get('KnowledgeArticleVersion');
            }
            // Create the wrapper only if data is found else just add 'No articles found' and return it back.
            if (!lstSearchResults.isEmpty()) {
                for (Search.SearchResult searchResult : lstSearchResults) {
                    KnowledgeArticleVersion objArticleVersion = (KnowledgeArticleVersion) searchResult.getSObject();
                    lstArticleWrapper.add(new KnowledgeArticleWrapper_ACE(String.valueOf(objArticleVersion.Id), objArticleVersion.Title, null, lstErrors));
                }
            } else {
                lstArticleWrapper.add(new KnowledgeArticleWrapper_ACE(null, 'No articles found', null, lstErrors));
            }
        } catch (Exception objException) {
            List<ErrorLog_CF__c> lstErrorLog = ErrorLogClass_CF.processExceptionAndReturnErrorLog(objException);
            String name = [SELECT Name FROM ErrorLog_CF__c WHERE Id = :lstErrorLog[0].Id].Name;
            lstErrors.add(name);
            lstArticleWrapper.add(new KnowledgeArticleWrapper_ACE(null, null, null, lstErrors));
        }
        return lstArticleWrapper;
    }

But when I put a debug statement in my class :

        if (!String.isEmpty(strSearchTerms)) {
            System.debug([SELECT Id, UrlName, PublishStatus FROM KnowledgeArticleVersion]);
            objSearchResults = Search.find('FIND :strSearchTerms IN ALL FIELDS RETURNING KnowledgeArticleVersion(Id, Title, UrlName' +
                    ' WHERE PublishStatus = \'Online\' ORDER BY CreatedDate DESC LIMIT 3)');
            lstSearchResults = objSearchResults.get('KnowledgeArticleVersion');
        }

It returns a result:

USER_DEBUG|[72]|DEBUG|(KnowledgeArticleVersion:{Id=ka07F000000a2vvQAA, UrlName=TestTitle, PublishStatus=Online})

BUt the SOSL is unable to find the KnowledgeArticleVersion object. The SOSL works fine in real scenario with real data, but does not work with test data.

It never enter this statement.

if (!lstSearchResults.isEmpty()) {
1
  • 1
    Did you execute setfixedsearchresults? In the SOSL docs it tells you what you need to call. It will only return what you set via that method
    – Eric
    Aug 24, 2018 at 6:55

1 Answer 1

11

Just as @Eric commented out: You need to place searched object id into Test.setFixedSearchResults

So in your case:

Id [] fixedSearchResults= new Id[1];
fixedSearchResults[0] = objKav.Id;
Test.setFixedSearchResults(fixedSearchResults);

After that you can SOSL objKav.

Reference:

SOSL Test Docs

0

You must log in to answer this question.

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