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
?
Best Answer
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:
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
: