April 15, 2012

Chatter Apex Test Case Bug in API version 24

I noticed this issue while writing an Apex test case for some chatter related functionality. For sake of this post, I am just posting the simplified test case. Here is the code

public static testmethod void testFeeds() {
  Test.startTest();
    // Create a Test Account
    Account acc = new Account(Name = 'Demo Account');
    insert acc;
    
    // Create FeedItem using this Account as a Parent Record
    FeedItem fp = new FeedItem();
    fp.Body = 'Testing via Apex';
    fp.parentId = acc.id;
    insert fp;
    
    // Query AccountFeed back to verify if the post was created correctly
    AccountFeed[] accFeed = [Select Id, Body From AccountFeed Where ParentId =:acc.Id];
    // should be 1 record in the feed for this accountid
    System.assertEquals(1, accFeed.size());
  Test.stopTest();
}

This test case fails, because there are no rows in AccountFeed for the given “parentId”. This happens only if Apex class API version is “24”, but the same tests passes if the API version is lowered, for ex. “23”.

Seems something is broken in Apex Test fixture, posted the same on discussion boards here for resolution.

UPDATE

After tweeting this issue @andyboettcher replied by a solution.

image

Thanks to Mr. Andy for suggesting the “SeeALLData” fix, this for sure fixes the problem in v24, but we loose the data isolation. I fixed original client requirement, by moving back to v23 and setting “SeeAllData=false”.