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
Abhinav GuptaAbhinav Gupta 

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.

Jia HuJia Hu

Use @isTest(SeeAllData=true) please,

 

default you cannot see the real data in test from Ver. 24

 

Document is here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

alexbalexb

I'm writing unit tests for a controller. In my controller, I'm inserting a record and attaching a chatter post to it. While I can query the record, I can *not* query the record's feed in v24. I am forced to use the @isTest(SeeAllData=true) version of the unit test in order to get it.

 

This doesn't make sense to me. If my controller inserts a chatter post, I should be able to query it out in my test. This is a little confusing and should be noted in the documenation if this is by design.

 

Thanks for bringing this topic up, OP.

Jia HuJia Hu

I tested based on your scenario.

 

In a apex, I insert a account and then insert a feed on this new account.

In the test class, without  (SeeAllData=true), I can access this account and the feed on this account.

 

If you can give more details, ....

Abhinav GuptaAbhinav Gupta

Would be nice if you can share the detail, I am interested to know, how it worked in API v24 test case and without using SeeAllData=true !

Jia HuJia Hu

Not sure if this is in your case, 

 

public with sharing class Apex_Test1 {

public static Id Account1() {
Account acc = new Account();
acc.Name = 'New ' + Datetime.now();
insert acc;

FeedItem fi = new FeedItem();
fi.Body = acc.Name;
fi.ParentId = acc.Id;
insert fi;

return acc.Id;
}

@isTest static void test1() {
Id id1 = Apex_Test1.Account1();
FeedItem fi = [Select Id, Body From FeedItem Where ParentId = : id1];
System.assertEquals( fi.Body, [Select Name From Account Where Id =: id1].Name );
}

}

Bhushan2710Bhushan2710

Hi ,

I have test method where i try to get the Feeditem of type trackedChange which is automatically created when we update a field value that is being tracked via feedTracking. But the test method is not able to get that feedItem record. Even tried with lowering the version, but no results. Can someone help?

Following is the testmethod: -

static testmethod void mytest(){
        Account acc = new Account(Name = 'TestAccountBhushan',Phone = '040-754-7845');
        insert acc;        
        acc.Phone = '040-123-4567';            
        update acc;      
        List<FeedItem > txtFeedItem = [SELECT Id,
               ParentId FROM FeedItem Where ParentId=: acc.Id];
               System.assertEquals(1, txtFeedItem.size());
        }