Testing ConnectApi Code

Like all Apex code, Chatter in Apex code requires test coverage.

Chatter in Apex methods don’t run in system mode, they run in the context of the current user (also called the context user user). The methods have access to whatever the current user has access to. Chatter in Apex does not support the runAs system method.

Most Chatter in Apex methods require access to real organization data, and fail unless used in test methods marked @IsTest(SeeAllData=true).

However, some Chatter in Apex methods, such as getFeedItemsFromFeed, are not permitted to access organization data in tests and must be used in conjunction with special test methods that register outputs to be returned in a test context. If a method requires a setTest method, the requirement is stated in the method’s “Usage” section.

A test method name is the regular method name with a setTest prefix. The test method signature (combination of parameters) matches a signature of the regular method. For example, if the regular method has three overloads, the test method has three overloads.

Using Chatter in Apex test methods is similar to testing Web services in Apex. First, build the data you expect the method to return. To build data, create output objects and set their properties. To create objects, you can use no-argument constructors for any non-abstract output classes.

After you build the data, call the test method to register the data. Call the test method that has the same signature as the regular method you’re testing.

After you register the test data, run the regular method. When you run the regular method, the registered data is returned.

Important

Important

You must use the test method signature that matches the regular method signature. When you call the regular method, if data wasn't registered with the matching set of parameters, you receive an exception.

This example shows a test that constructs an ConnectApi.FeedItemPage and registers it to be returned when getFeedItemsFromFeed is called with a particular combination of parameters.
global class NewsFeedClass {
    global static Integer getNewsFeedCount() {
        ConnectApi.FeedItemPage items = 
            ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, 
                   ConnectApi.FeedType.News, 'me');
        return items.items.size();
    }
}
@isTest
private class NewsFeedClassTest {
    @IsTest
    static void doTest() {
        // Build a simple feed item
        ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
        List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
        testItemList.add(new ConnectApi.FeedItem());
        testItemList.add(new ConnectApi.FeedItem());
        testPage.items = testItemList;
        
        // Set the test data
        ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, 
                   ConnectApi.FeedType.News, 'me', testPage);

        // The method returns the test page, which we know has two items in it.
        Test.startTest();
        System.assertEquals(2, NewsFeedClass.getNewsFeedCount());
        Test.stopTest();
    }
}
Previous
Next