3

I have created a webservice APEX class that I need to write a Unit test for. The webservice assembles a custom filtered list of Chatter 'FeedElements'. All 'FeedElements' are system created, i.e. they post LogACall, Task and Event creation, adding a Note and Field Updates.

List<Id> ids = ... pre-populated list ...

List<Id> feedIds = new List<Id>();
for (List<FeedItem> items : [SELECT Id
                               FROM FeedItem
                              WHERE parentId IN :ids
                                AND Type IN (:String.valueOf(ConnectApi.FeedItemType.CallLogPost), 
                                             :String.valueOf(ConnectApi.FeedItemType.ContentPost), 
                                             :String.valueOf(ConnectApi.FeedItemType.CreateRecordEvent),
                                             :String.valueOf(ConnectApi.FeedItemType.TrackedChange))
                              ORDER BY CreatedDate DESC]) {
    for (FeedItem item : items) {
        feedIds.add(item.Id);
    }
}

for (ConnectApi.BatchResult res : ConnectApi.ChatterFeeds.getFeedElementBatch(null, feedIds)) {
    if (res.isSuccess()) {
        ... never reached in unit test ...
    }
}

Now, when creating Tasks, LogACalls, Events, Notes and updating fields by issuing DML statements in my Test Case, no FeedItems are being created. I tried to create my test data by invoking the according QuickActions from APEX, with the very same result.

When I was doing a manual insert of FeedItems, for each of these the ConnectApi.ChatterFeeds.getFeedElementBatch() method returned an error (NotFoundException).

Does somebody have an idea on how to create correct chatter feed test data of the types CallLogPost, ContentPost (Note), CreateRecordEvent (Task, Event) and TrackedChange?

1

Unfortunately it's not possible to create these types of feed items in unit tests, for the reasons described in the documentation for Isolation of Test Data from Organization Data in Unit Tests:

There might be some cases where you can’t create certain types of data from your test method because of specific limitations. Here are some examples of such limitations.

  • Records that are created only after related records are committed to the database, like tracked changes in Chatter. Tracked changes for a record (FeedTrackedChange records) in Chatter feeds aren't available when test methods modify the associated record. FeedTrackedChange records require the change to the parent record they're associated with to be committed to the database before they're created. Since test methods don't commit data, they don't result in the creation of FeedTrackedChange records. Similarly, field history tracking records (such as AccountHistory) can't be created in test methods because they require other sObject records to be committed first (for example, Account).

In light of this limitation, you'd have to assume that some system-generated feed item types are already present in your org. Here's an example for feed items of type TrackedChange:

@isTest(SeeAllData=true)
public class GetFeedElementBatchTest {

    static testMethod void testGetFeedElementBatch() {
        Integer maxItems = 2;
        List<FeedItem> feedItems = [SELECT Id FROM FeedItem WHERE Type='TrackedChange' LIMIT :maxItems];

        List<Id> feedItemIds = new List<Id>();
        for (FeedItem item : feedItems) {
            feedItemIds.add(item.Id);
        }

        List<ConnectApi.BatchResult> results = ConnectApi.ChatterFeeds.getFeedElementBatch(null, feedItemIds);
        System.assertEquals(maxItems, results.size());
        for (ConnectApi.BatchResult result : results) {
            System.assert(result.isSuccess());
            ConnectApi.FeedItem item = (ConnectApi.FeedItem) result.getResult();
            System.assertEquals(ConnectApi.FeedItemType.TrackedChange, item.type);
        }
    }

}
  • Thanks for pointing me to the documentation. Too bad, this makes it quite difficult to deploy tests as I have to deal with real data that might exist or not. – the_architect Oct 10 '16 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.