function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Karthikeyan ChandranKarthikeyan Chandran 

Test Class for Chatter Feed.


I have developed a Scheuled Apex Class for Chatter Feed Post.
Here, The sample code
for(integer i=0;i<cbday.size();i++){
         feeditem FI1=new feeditem();
         FI1.Body = 'Happy BirthDay - '+cbday[i].Name+'.';
         insert FIbday;

Can anyone help me out  to increase the test coverage on this feed item.

Thanks & Regards,
Karthikeyan Chandran.
Best Answer chosen by Karthikeyan Chandran
Abhinav GuptaAbhinav Gupta
First of all for scheduled Apex, you can use Test.startTest () and Test.stopTest() to wrap your test code for ex. 

Then you can query UserFeed for the newly create FeedItem Id, tried to explain the same in example code below.

// .... Call your Actual code to create scheduled chatter post
// It would be great if  you can get handle to newly create feeditems ids to validate later

// Test.stopTest will ensure that scheduled job is completed 

// Here query the feed to validate if the required Feed is created correctly.
// Here you can pass either single feed item id or multiple feed item ids
UserFeed uf = [Select ParentId, Id, Body From UserFeed  Where Id = :FI1.Id];
System.assertEquals ('Your text in chatter', uf.Body);

// Please note, I used UserFeed assuming the post is created on User's feed, if thats not the case, please select the appropriate Account, Case or other Custom object feed. Use Salesforce Schema app in Eclipse to see available tables and build a query easily.