I need to get trending articles from the community. I created a apex class for that by using ConnectApi.Knowledge.getTrendingArticles(communityId, maxResult).

I need to create a test class for that. I am using test class method provided by Salesforce for that. setTestGetTrendingArticles(communityId, maxResults, result) but I am getting this error

System.AssertException: Assertion Failed: No matching test result found for Knowledge.getTrendingArticles(String communityId, Integer maxResults). Before calling this, call Knowledge.setTestGetTrendingArticles(String communityId, Integer maxResults, ConnectApi.KnowledgeArticleVersionCollection result) to set the expected test result.

public without sharing class ConnectTopicCatalogController {
    public static List<ConnectApi.KnowledgeArticleVersion> getAllTrendingArticles(){
        string commId = [Select Id from Network where Name = 'Customer Community v5'].Id;
        ConnectApi.KnowledgeArticleVersionCollection mtCollection = ConnectApi.Knowledge.getTrendingArticles(commId, 12);
        System.debug('getAllTrendingTopics '+JSON.serializePretty(mtCollection.items));
        List<ConnectApi.KnowledgeArticleVersion> topicList = new List<ConnectApi.KnowledgeArticleVersion>();
        for(ConnectApi.KnowledgeArticleVersion mtopic : mtCollection.items)
            String kmType = [SELECT KM_Type__c from Knowledge__kav WHERE id =: mtopic.id].KM_Type__c ;
            if(kmType != 'Class3 Error'){
       return topicList;

Apex test class for this

public class ConnectTopicCatalogControllerTest {
    public static final string communityId = [Select Id from Network where Name = 'Ansys Customer Community'].Id;
    static void getTrendingArticles(){
        ConnectApi.KnowledgeArticleVersionCollection knowledgeResult = new ConnectApi.KnowledgeArticleVersionCollection();
        Knowledge__kav knowledgeData = [Select Id, knowledgeArticleId, Title, Summary, UrlName from Knowledge__kav Limit 1];
        List<ConnectApi.KnowledgeArticleVersion> know = new List<ConnectApi.KnowledgeArticleVersion>();
        know.add(new ConnectApi.KnowledgeArticleVersion());
        know.add(new ConnectApi.KnowledgeArticleVersion());
        knowledgeResult.items = know;
        // Set the test data
        ConnectApi.Knowledge.setTestGetTrendingArticles(null, 12, knowledgeResult);
        List<ConnectApi.KnowledgeArticleVersion> res = ConnectTopicCatalogController.getAllTrendingArticles();
        // The method returns the test page, which we know has two items in it.
        System.assertEquals(12, res.size());

Reference to create test class for ConnectApi https://developer.salesforce.com/docs/atlas.en-us.238.0.apexcode.meta/apexcode/connectAPI_TestingApex.htm


1 Answer 1


Welcome to Salesforce Stack Exchange (SFSE)!

I am unfamiliar with Communities/Experience Cloud, I don't have an org available with Communities/Experience to test my hypothesis, and I haven't found a definitive statement in my interwebs searches. But I assume that Network is considered a "setup object" and thus its data is visible in a test context.

My assumption is bolstered by the fact that you do not seem to be getting a...

List has no rows for assignment to SObject

...error on this line...

string commId = [Select Id from Network where Name = 'Customer Community v5'].Id;

...during your test.

In a box on the page to which you link there is this statement:

Use the test method signature that matches the regular method signature. If data wasn't registered with the matching set of parameters when you call the regular method, you receive an exception.

[Emphasis added by me]

Your setTest call:

ConnectApi.Knowledge.setTestGetTrendingArticles(null, 12, knowledgeResult);

Your get call:

ConnectApi.Knowledge.getTrendingArticles(commId, 12)

So if Network data is, indeed, test visible, you are setting your test data with a null Community Id value, and yet your code is still using the Id of the 'Customer Community v5' Community.

You must log in to answer this question.

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