4

I have implemented a VF page that displays a Chatter Group feed using the example classes provided in the Salesforce Cookbook. I have implemented all of these classes (with some tweaks and added functionality) except for the last 2 (FeedBodyParser and FeedItemPoster) as they were not needed for this project.

This page renders and works without errors and is included within another VF page. I have also extended the DemoController to add some functionality using 's. I have written tests for the getters and setters of all of the class variables for my controller extension and they pass. However, I am having extreme difficulty with testing any of the methods that rely on ConnectApi classes in each of the classes I have implemented from the Cookbook since most of them rely on having an existing ChatterFeed to work with and because I'm using Chatter with Apex, I do not have access to organizational data.

I have tried to implement a test class following the example in the Salesforce doc: Testing ConnectApi Classes, but I keep getting an error when trying to call my Controller methods after using the setTestGetFeedItemsFromFeed() method that states that my controller method does not exist or has an incorrect signature.

I need to be able to create a feed and test against it for the FeedItemInfo, CommentInfo, etc. classes. Any help would be greatly appreciated as I am nowhere near the 75% code coverage required for this project without being able to create these tests.

Here's a look at my Code: Starting with my Controller(tweaked from DemoController)

global class CanNewsChatterFeedController {
    // get first page of news feed
    global ConnectApi.FeedItemPage getNewsFeed() {
        return ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.Record, '0F9d00000000fJc');
    }

    // build list of wrapped feed items for display in VisualForce
    global List<FeedItemInfo> getNewsFeedForDisplay() {
        ConnectApi.FeedItemPage feed = getNewsFeed();      
        List<FeedItemInfo> result = new List<FeedItemInfo>();
        for (ConnectApi.FeedItem item : feed.items) {
            result.add(new FeedItemInfo(item));
        }

        return result;
    }

    global PageReference refreshFeed(){
        return null;
    }
}

My controller extension:

public with sharing class CanNewsChatterFeedExtension {
    //declare class variables
    public String feedItemId; 
    public String likeId;  
    public String commentToAdd; 
    public Integer pollChoicePosition; 

    // Constructor. Needed to use as an extension.
    public CanNewsChatterFeedExtension(CanNewsChatterFeedController custController) {}

    //call via javascript and actionFunction. Class variables feedItemId and commentToAdd are set by passing params via actionFunction
    public void addCommentToFeedItem(){
        try{
            FeedComment fcomment = new FeedComment();
            fcomment.FeedItemId = feedItemId; 
            fcomment.CommentBody = commentToAdd; 
            insert fcomment;
        }catch(Exception e){
            System.debug(e.getMessage());
        }
    }

    //call via javascript and actionFunction. Class variable feedItemId is set by passing params via actionFunction
    public void addLikeToFeedItem(){
        try{
            ConnectApi.ChatterFeeds.likeFeedItem(null, this.feedItemId);
        }catch(Exception e){
            System.debug(e.getMessage());
        }
    }

    //call via javascript and actionFunction. Class variable likeId is set by passing params via actionFunction
    public void removeLikeFromFeedItem(){
        try{
            ConnectApi.ChatterFeeds.deleteLike(null, this.likeId);
        }catch(Exception e){
            System.debug(e.getMessage());
        }
    }

    //call via javascript and actionFunction. Class variables feedItemId and pollChoicePosition are set by passing params via actionFunction
    public void addVoteToFeedItemPoll(){
        try{
            ConnectApi.FeedPoll thePoll = ConnectApi.ChatterFeeds.getFeedPoll(null, this.feedItemId);
            String pollChoiceId = thePoll.choices[this.pollChoicePosition-1].id;
            ConnectApi.ChatterFeeds.voteOnFeedPoll(null, this.feedItemId, pollChoiceId);
        }catch(Exception e){
            System.debug(e.getMessage());
        }
    }

    //begin getters
    public String getFeedItemId(){
        return this.feedItemId;    
    }
    public String getLikeId(){
        return this.likeId;    
    }
    public String getCommentToAdd(){
        return this.commentToAdd;    
    }
    public Integer getPollChoicePosition(){
        return this.pollChoicePosition;    
    }
    //begin setters
    public void setFeedItemId(String theId){
        this.feedItemId = theId;
    }
    public void setLikeId(String theId){
        this.likeId = theId;
    }
    public void setCommentToAdd(String theComment){
        this.commentToAdd = theComment;
    }
    public void setPollChoicePosition(Integer thePosition){
        this.pollChoicePosition = thePosition;
    }
}

My Controller Test Class:

@isTest
public class CanNewsChatterFeedTest{
    @isTest
    static void chatterFeedTest(){
        ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
        ConnectApi.FeedItem feedItem = new ConnectApi.FeedItem();
        ConnectApi.Comment comment = new ConnectApi.Comment();

        List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
        testItemList.add(feedItem);
        testPage.items = testItemList;

        ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, ConnectApi.FeedType.News, 'me', testPage);

        Test.startTest();
        System.assert(CanNewsChatterFeedController.getNewsFeed() instanceof ConnectApi.FeedItemPage);
        //keep getting error here that states:Method does not exist or incorrect signature: CanNewsChatterFeedController.getNewsfeed()
        Test.stopTest();
    }
}

My Controller extension Test Class:

@isTest
public class CanNewsChatterFeedExtensionTest{
    @isTest
    static void canNewsChatterFeedExtensionTest(){
        //Instantiate and construct the controller class.   
        CanNewsChatterFeedExtension controller = new CanNewsChatterFeedExtension(new CanNewsChatterFeedController()); 

        //set class variables and assert that the getters return the expected values
        controller.setFeedItemId('theFeedItemId');
        System.assertEquals('theFeedItemId', controller.getFeedItemId());

        controller.setLikeId('theLikeId');
        System.assertEquals('theLikeId', controller.getLikeId());

        controller.setCommentToAdd('Test comment.');
        System.assertEquals('Test comment.', controller.getCommentToAdd());

        controller.setPollChoicePosition(1);
        System.assertEquals(1, controller.getPollChoicePosition());

       //setter and getter test work great, but how to test other methods called by apex:actionFunction?
    }
}

First attempt at the CommentInfo Test class:

@isTest
public class CommentInfoTest{
    @isTest
    static void commentInfoTest(){
        //instantiate and construct a ConnectApi.Comment object
        ConnectApi.Comment inComment = new ConnectApi.Comment();

        //instantiate and construct a CommentInfo object
        CommentInfo theCommentInfo = new CommentInfo(inComment);
        //set class variables and assert that the getters return the expected values
        //Somehow need to figure out how to instantiate a feed to get comments from for testing this
    }
}

I believe my problems stem from the fact that I need to simulate organization data for my tests of these classes, but the error I'm getting after trying to use the setTestGetFeedItemsFromFeed() method is preventing me from building the data I need.

  • Not sure why my Test Class code broke like that when I posted, but if anyone knows, please let me know for future posts. – steve.westfall Aug 22 '13 at 15:33
  • 2
    After copy-pasting your code, highlight the entire section of code and press Ctrl-K. This will indent the code correctly so that it doesn't break. You can fix it by editing your post. – sfdcfox Aug 22 '13 at 15:58
  • I went ahead and edited your post, but essentially SFSE uses 4 spaces to indicate a line of code. The beginning and ending of all of your classes weren't indented the 4 spaces. – Jesse Altman Aug 22 '13 at 15:59
  • Thanks @JesseAltman. Any suggestions on a solution for my issue – steve.westfall Aug 22 '13 at 17:11
  • Anyone? No suggestions? – steve.westfall Aug 27 '13 at 14:21
2

The parameter values need to match exactly. Because your controller is calling

ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.Record, '0F9d00000000fJc');

you need to call

ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, ConnectApi.FeedType.Record, '0F9d00000000fJc', testPage);

in your test to register the test data, instead of

ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, ConnectApi.FeedType.News, 'me', testPage);
  • Thanks @alouie! That worked like a charm. Sorry it took so long to accept your answer. I was on vacation. – steve.westfall Sep 9 '13 at 15:03
0

You're calling CanNewsChatterFeedController.getNewsFeed() from the test class statically, but it's not defined as a static method. Try using

global static ConnectApi.FeedItemPage getNewsFeed() {
    ...
}

I did this in my dev org and it fixed the compiler error.

  • Thanks @alouie. I tried that, but then when I run the test, I get a different error that states: Error Message System.AssertException: Assertion Failed: No matching test result found for ChatterFeeds.getFeedItemsFromFeed(String communityId, ConnectApi.FeedType feedType, String subjectId). Before calling this, call ChatterFeeds.setTestGetFeedItemsFromFeed(String communityId, ConnectApi.FeedType feedType, String subjectId, ConnectApi.FeedItemPage result) to set the expected test result. – steve.westfall Aug 28 '13 at 12:44
  • I get the above mentioned error even though I clearly call the setTestGetFeedItemsFromFeed() method directly before the Test.startTest() call. Could this be because I'm in a developer org and not a sandbox? – steve.westfall Aug 28 '13 at 17:32
  • The problem is that the parameter values need to match exactly. I'll add another answer below, because I don't think I can format things the way I want in a comment. – alouie Aug 28 '13 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.