7

I've been running a chatter tutorial and ran across something I don't understand.

First off here's the link to the tutorial .

The following code is straight out of the tutorial and works, except for the test method. The test method fails on the first assert() every time. The reason it fails is because the array is always empty.

Why is the test method failing but the visualforce page works fine? Better yet, why isn't the getRecentTextUpdates() returning anything in the test method?

<apex:page controller="NewsFeedController" sidebar="false">
  <h1>News Feed</h1>
  <apex:form >
      <apex:inputText value="{!feedUpdate}"/>
      <apex:commandButton value="Update" action="{!go}" reRender="recent"/>

      <apex:outputPanel id="recent">
          <apex:dataList value="{!recentTextUpdates}" var="update">
              <apex:outputText value="{!update.body}"></apex:outputText>
          </apex:dataList>
      </apex:outputPanel>
  </apex:form>
</apex:page> 

public class NewsFeedController {

    @isTest
    static void testUpdates() {
        NewsFeedController con = new NewsFeedController();
        con.feedUpdate = 'Hi';
        con.go();
        List<NewsFeed> newsFeed = con.getRecentTextUpdates();
        System.debug('My NewsFeed Size: ' + newsFeed.size());
        System.assert(newsFeed.size() > 0);
        System.assertEquals('Hi', newsFeed[0].Body);
    }

    public PageReference go() {
        FeedItem feedItem = new FeedItem();
        feedItem.Type = 'TextPost';
        feedItem.ParentId = UserInfo.getUserId();
        feedItem.Body = this.feedUpdate;
        insert feedItem;

        return null;
    }

    public List<NewsFeed> getRecentTextUpdates() {
        List<NewsFeed> newsFeed = [SELECT Type, CreatedDate, CreatedBy.Name, Body FROM NewsFeed WHERE Type = 'TextPost' ORDER BY CreatedDate DESC LIMIT 10];

        return newsFeed;
    }


    public String feedUpdate { get; set; }
}
5
  • 1
    Changing NewsFeed to FeedItem in the test method and getRecentTextUpdates() makes the test pass. Not sure why NewsFeed won't work - coz Users should be able to only query their own NewsFeed. Unless there are limitations on querying in a Test Method. I tried System.runAs to no avail Oct 27, 2012 at 22:51
  • thanks, FeedItem works when replacing the NewsFeed. Pretty Odd.
    – fourq
    Oct 27, 2012 at 23:02
  • @techtrekker still, I have no idea why NewsFeed works in normal runtime but not in the testing context.
    – fourq
    Oct 27, 2012 at 23:12
  • It is odd. I wonder if it has something to do with "Users can only query their own news feed". System.runAs isnt cutting it either. Oct 27, 2012 at 23:18
  • well even so, it's the same user inserting and querying it in this case.
    – fourq
    Oct 27, 2012 at 23:20

2 Answers 2

4

You might need to check API version, I faced similar issue in API v24 update. I have to lower down API version 23 to get rid of it. More details in this blog post here : http://www.tgerm.com/2012/04/chatter-apex-test-case-bug-in-api.html

8
  • So definitely a bug then, wonder if salesforce know about this. Nothing on the Known Issues Index Oct 28, 2012 at 9:17
  • True, I think its not listed yet here : sites.secure.force.com/success/apex/issues_index Oct 28, 2012 at 9:39
  • @AbhinavGupta changing the Controller class to API 23 fixed the issue. Anything after 23 I had to replace NewsFeed with FeedItem in order to get the test method to work.
    – fourq
    Oct 28, 2012 at 14:32
  • Did either of you guys list this as an issue? If Not, I will.
    – fourq
    Oct 28, 2012 at 14:39
  • Please list that or post as an idea @fourq, as I don't see a way to post issue on the issue list page(it seems internal) Oct 28, 2012 at 16:27
6

From API version 24, Salesforce stop supporting real records in test cases. To access the real records, add the tag (SeeAllData=true) in your test class,

@isTest(SeeAllData=true)
public class TestCase{
  static testMethod void TestMethodOne() { 
  }
}
1
  • Ah, that's it! I was trying all various combinations. Saved my day. Dec 15, 2013 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .