[SalesForce] How to write tests for custom classes that primarily use ConnectApi

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.

Best Answer

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);
Related Topic