Newer Version Available

This content describes an older version of this product. View Latest

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 or the logged-in 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 method calls 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. A test method name is the regular method name with a setTest prefix. The test method has a signature (combination of arguments) to match every signature of the regular method. If the regular method has three overloads, the test method has three overloads. If a method requires a setTest method, the requirement is stated in the method’s “Usage” section.

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 value that was registered with matching arguments is returned.

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 arguments, you receive an exception.

Important

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.
1swfobject.registerObject("clippy.codeblock-0", "9");global class NewsFeedClass {
2    global static Integer getNewsFeedCount() {
3        ConnectApi.FeedItemPage items = 
4            ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, 
5                   ConnectApi.FeedType.News, 'me');
6        return items.items.size();
7    }
8}
1swfobject.registerObject("clippy.codeblock-1", "9");@isTest
2private class NewsFeedClassTest {
3    @IsTest
4    static void doTest() {
5        // Build a simple feed item
6        ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
7        List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
8        testItemList.add(new ConnectApi.FeedItem());
9        testItemList.add(new ConnectApi.FeedItem());
10        testPage.items = testItemList;
11        
12        // Set the test data
13        ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, 
14                   ConnectApi.FeedType.News, 'me', testPage);
15
16        // The method returns the test page, which we know has two items in it.
17        Test.startTest();
18        System.assertEquals(2, NewsFeedClass.getNewsFeedCount());
19        Test.stopTest();
20    }
21}