function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Oliver Westlake-SimmOliver Westlake-Simm 

Creating Test Class for Einstein Bot Invocable Apex Class to Search Articles

I've created an Invocable Apex Class that allow us to search articles and return the resolution to the bot. The Apex class works as expected in the sandbox. However, I'm struggling to create a test class with more then 68% code coverage. Could anyone help, please?

APEX CLASS
public with sharing class Bot_SearchFAQ {
    public class FAQSearchInput{
        @InvocableVariable(required=true)
        public String sKeyword;
    }
    public class FAQSearchOutput{
        @InvocableVariable(required=true)
        public String sFAQSearchResult;
    }
    @InvocableMethod(label='Search FAQ')
    public static List<FAQSearchOutput> searchFAQ(List<FAQSearchInput> faqSearchInput) {
        String sArticleBaseUrl = getCommunityArticleBaseUrl();
        String sKeyword = faqSearchInput[0].sKeyword;
        String sQuery = 'FIND \'' + String.escapeSingleQuotes(sKeyword) + '\' IN NAME FIELDS RETURNING Knowledge_Article__kav(Id, Title, Chatbot_URL__c, Chatbot_Resolution__c WHERE PublishStatus = \'Online\' AND Language = \'en_US\' AND IsVisibleInPkb = true) WITH SNIPPET (target_length=255) LIMIT 1';
        System.debug(sQuery);
        Search.SearchResults searchResults = Search.find(sQuery);
        List<Search.SearchResult> articlelist = searchResults.get('Knowledge_Article__kav');
        System.debug('articlelist: ' + articlelist);
        String sFAQSearchResult = '';
        for (Search.SearchResult searchResult : articlelist)        {
            Knowledge_Article__kav article = (Knowledge_Article__kav)searchResult.getSObject();
            String sArticleSummary;
            //String sSnippet = searchResult.getSnippet('Knowledge_Article__kav.Resolution__c');
            String sSnippet = article.Chatbot_Resolution__c;
            sArticleSummary = summarizeArticleForBot(sArticleBaseUrl, article, sSnippet);
            System.debug('sArticleSummary: ' + sArticleSummary);
            sFAQSearchResult = sFAQSearchResult + sArticleSummary;
        }
        if (sFAQSearchResult == '') sFAQSearchResult = 'No result found.';
        List<FAQSearchOutput> faqSearchOutputs = new List<FAQSearchOutput>();
        FAQSearchOutput faqSearchOutput = new FAQSearchOutput();
        faqSearchOutput.sFAQSearchResult = sFAQSearchResult;
        faqSearchOutputs.add(faqSearchOutput);
        system.debug(faqSearchOutput);
        return faqSearchOutputs;
    }

    public static String summarizeArticleForBot(String sArticleBaseUrl, Knowledge_Article__kav article, String sSnippet){
        String sSummary, sURL;
        sURL = article.Chatbot_URL__c;
        //remove highlight HTML tag <mark>
        sSummary = 'Here is what I found: ' + sSnippet.replaceAll('<[^>]+>',' ') + '\n' + 'To read more click here:\n' + sURL + '\n';
        return sSummary;
    }
    public static string getCommunityArticleBaseUrl()
    {
        List<Network> communityNetworks = [SELECT Id FROM Network WHERE Name='Community Support'];
        String sArticleBaseUrl = '';
        if (communityNetworks.size()>0)
        {
            Network communityNetwork = communityNetworks[0];
            String sLoginUrl = Network.getLoginUrl(communityNetwork.id);
            sArticleBaseUrl = sLoginUrl.replace('/login', '/article/');
            System.debug('MyDebug - Community Login URL: ' + sLoginUrl);
            System.debug('MyDebug - Article Base URL: ' + sArticleBaseUrl);
        }
        return sArticleBaseUrl;
    }
}

TEST CLASS
@isTest
private class Bot_SearchFAQ_Test{
  @testSetup
  static void setupTestData(){   
    Knowledge_Article__kav knowledge_article_kav_Obj = new Knowledge_Article__kav(IsVisibleInPkb = true, Language = 'en_US', Title = 'Test Text?', Details__c='Test Text.', UrlName = 'UrlName760', Chatbot_Resolution__c = 'Test Text', Chatbot_URL__c = 'http://test71.com');
    Insert knowledge_article_kav_Obj;
    Knowledge_Article__kav newArticle = [SELECT Id, KnowledgeArticleId FROM Knowledge_Article__kav WHERE id=:knowledge_article_kav_Obj.Id];
    String articleId = newArticle.KnowledgeArticleId;
    KbManagement.PublishingService.publishArticle(articleId, true);
  }
  static TestMethod void test_searchFAQ_UseCase1(){
    test.startTest();

    Bot_SearchFAQ.faqSearchInput input = new Bot_SearchFAQ.faqSearchInput();
    input.sKeyword = 'Test Text?';

    List<Bot_SearchFAQ.faqSearchInput> listInput = new List<Bot_SearchFAQ.faqSearchInput>();
    listInput.add(input);

    Bot_SearchFAQ.searchFAQ(listInput);

    test.stopTest();
  }
}
Dylan McSparren 8Dylan McSparren 8
very very late here but have just started working on the same type of project, adding a negative test to force 'No result found.'  should help to cover another line or two. Using input like 'nothing' instead of 'Test Text?'