4

This is actually an extension of my initial question about writing tests for custom classes that use primarily ConnectApi classes. I'm asking it in it's own space because there's a lot of info to provide. For a little further information, helpful do-gooders will probably want to read the first question linked above for context. Thanks in advance to all who do.

Since posting the first question and getting a great answer from @[alouie], I have reconsidered the hard-coding of the Chatter group Id in the ConnectApi method call used to get the required chatter feed. Instead, I have added a call to the ConnectApi.ChatterGroups.searchGroups() method to find the chatter group by name so that I can retrieve the unique Id of the group regardless of which environment I'm in (developer instance/sandbox/production). This course of action works great and I am able to get the feed, display it and all my other custom functionality works fine.

Once again, my issue is with writing the test methods for this in order to get my required 75% code coverage. As mentioned above, I was able to implement the suggestion from @[alouie] from my initial question and all my tests ran fine. However, when I added in a call to ConnectApi.ChatterGroups.setTestSearchGroups() to set-up the test data, I keep getting the following error when running my tests:

System.AssertException: Assertion Failed: No matching test result found for ChatterGroups.searchGroups(String communityId, String q, Integer pageParam, Integer pageSize). Before calling this, call ChatterGroups.setTestSearchGroups(String communityId, String q, Integer pageParam, Integer pageSize, ConnectApi.ChatterGroupPage result) to set the expected test result.

I am calling this in my test class right before the call to setTestGetFeedItemsFromFeed() but for some reason I still get the error. Do the setTest methods cancel each other out? Does anyone have any idea why my test result is not being found?

Here is the new code for my controller:

public class CanNewsChatterFeedController {
    //empty constructor
    public CanNewsChatterFeedController(){}

    // get first page of news feed
    public static ConnectApi.FeedItemPage getNewsFeed() {
        //get the proper Chatter Group Page
        ConnectApi.ChatterGroupPage chatterGrpPg = getCanNewsChatterGrpPg();
       //get the groupId
       String theGroupId = chatterGrpPg.groups[0].id;
       //use the groupId to get the feedItems
       ConnectApi.FeedItemPage feedPage = ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.Record, theGroupId);
       return feedPage;
    }

    //The Id of the ChatterGroup we want to display will be different in the sandbox from what it is in production
    //(production id: 0F9D0000000UGqS in sandbox id:0F9c0000000Chu1), in order to get the feed items we want, 
    //we have to search for the ChatterGroup by name. Since Group Name must be unique,
    //the searchGroups method should only return one ChatterGroupPage with one ChatterGroup
    public static ConnectApi.ChatterGroupPage getCanNewsChatterGrpPg(){
        return ConnectApi.ChatterGroups.searchGroups(null, 'Canada - Corby Scoreboard', null, 1);
    }    

    // build list of wrapped feed items for display in VisualForce
    public static 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;
    }


    public PageReference refreshFeed(){
        return null;
    }
}

And here is the code for my test class:

@isTest
public class CanNewsChatterFeedTest{
    @isTest
        static void doTest(){
            //create some test properties for testing ConnectApi classes
            final String FEED_ITEM_ID = 'feedItemId';
            final String LIKE_ID = 'theLikeId';
            //the following variable will need to be changed to 0F9D0000000UGqS prior to production move
            final String SCOREBOARD_GROUP_ID = '0F9d00000000fJc';
            final String SCOREBOARD_GROUP_NM = 'Canada - Corby Scorecard';
            final String USER_ID = 'theUserId';
            final String USER_NM = 'theUserName';
            final String CONTENT_DWNLD_URL = '/sfc/servlet.shepherd/version/download/1.0?asPdf=false&operationContext=CHATTER';

            //Instantiate and construct an instance of CanNewsChatterFeed
            CanNewsChatterFeedController controller = new CanNewsChatterFeedController();
            System.assertEquals(null, controller.refreshFeed());

            //Instantiate and construct an instance of CanNewsChatterFeedExtension.   
            CanNewsChatterFeedExtension controllerExt = new CanNewsChatterFeedExtension(controller); 

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

            controllerExt.setLikeId(LIKE_ID);
            System.assertEquals(LIKE_ID, controllerExt.getLikeId());

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

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

            //Build a simple feed item page
            ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
            List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
            ConnectApi.FeedItem testFeedItem = new ConnectApi.FeedItem();
            testFeedItem.id = FEED_ITEM_ID;
            testFeedItem.relativeCreatedDate = 'Just now.';
            ConnectApi.FeedBody testFeedBody = new ConnectApi.FeedBody();
            List<ConnectApi.MessageSegment> testSegments = new List<ConnectApi.MessageSegment>();
            ConnectApi.MessageSegment testSegment = new ConnectApi.TextSegment();
            testSegment.text = 'This is the feedBody.';
            testSegments.add(testSegment);
            testFeedBody.messageSegments = testSegments;
            testFeedItem.body = testFeedBody;
            //create a Like and add it to the feedItem
            ConnectApi.ChatterLikePage testLikePage = new ConnectApi.ChatterLikePage();
            List<ConnectApi.ChatterLike> testLikes = new List<ConnectApi.ChatterLike>();
            ConnectApi.ChatterLike testLike =  new ConnectApi.ChatterLike();
            ConnectApi.Reference testMyLikeRef = new ConnectApi.Reference();
            ConnectApi.Reference testFeedItemRef = new ConnectApi.Reference();
            testMyLikeRef.id = LIKE_ID;
            testLike.id = LIKE_ID;
            testFeedItemRef.id = LIKE_ID;
            testLike.likedItem = testFeedItemRef;
            testLikes.add(testlike);
            testLikePage.likes = testLikes;
            testFeedItem.likes = testLikePage;
            testFeedItem.isLikedByCurrentUser = true;
            testFeedItem.myLike = testMyLikeRef;
            ConnectApi.MessageBody testLikesMessage = new ConnectApi.MessageBody();
            testLikesMessage.text = 'You like this.';
            testFeedItem.likesMessage = testLikesMessage;

            //create a comment and add it to the feedItem
            ConnectApi.Comment testComment = new ConnectApi.Comment();
            ConnectApi.FeedBody testCommentBody = new ConnectApi.FeedBody();
            List<ConnectApi.MessageSegment> testCommentSegments = new List<ConnectApi.MessageSegment>();
            ConnectApi.MessageSegment testCommentSegment = new ConnectApi.TextSegment();
            testCommentSegment.text = 'This is the comment feedBody.';
            testCommentSegments.add(testSegment);
            testCommentBody.messageSegments = testCommentSegments;
            testComment.body = testCommentBody;
            ConnectApi.CommentPage testCommentPage = new ConnectApi.CommentPage();
            List<ConnectApi.Comment> testComments = new List<ConnectApi.Comment>();
            testComments.add(testComment);
            testCommentPage.comments = testComments;
            testFeedItem.comments = testCommentPage;

            //create a chatterGroup page
            ConnectApi.ChatterGroupPage testChatterGrpPg = new ConnectApi.ChatterGroupPage();
            List<ConnectApi.ChatterGroupDetail> testGroups = new List<ConnectApi.ChatterGroupDetail>();
            ConnectApi.ChatterGroupDetail testGroup =  new ConnectApi.ChatterGroupDetail();
            testGroup.id = SCOREBOARD_GROUP_ID;
            testGroups.add(testGroup);
            testChatterGrpPg.groups = testGroups;

            //create a parent feedItem for our testFeedItem
            testFeedItem.parent = testChatterGrpPg.groups[0];
            testFeedItem.parent.motif = new ConnectApi.Motif();
            testFeedItem.parent.motif.smallIconUrl = 'http://www.smallicon.com/icon.jpg';

            //create an attachment for our testFeedItem
            ConnectApi.ContentAttachment testAttachment = new ConnectApi.ContentAttachment();
            testAttachment.renditionUrl = 'http://www.theRenditionUrl.com';
            testAttachment.description = 'theAttachmentDescription';
            testAttachment.title = 'theAttachmentTitle';
            testAttachment.hasImagePreview = false;
            testAttachment.versionId = '1.0';
            testFeedItem.attachment = testAttachment;
            testItemList.add(testFeedItem);
            testPage.items = testItemList;

            // Set the test data
            ConnectApi.ChatterGroups.setTestSearchGroups(null, SCOREBOARD_GROUP_NM, null, 1, testChatterGrpPg);
            ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(null, ConnectApi.FeedType.Record, SCOREBOARD_GROUP_ID, testPage);

            // The method returns the test page, which we know has one item in it.
            // Test the size is correct and then test the attributes of the feed item
            Test.startTest();
                ConnectApi.FeedItemPage testFeed = CanNewsChatterFeedController.getNewsFeed();
                System.assertEquals(1, testFeed.items.size());
                //simulate the call to CanNewsChatterFeedController.getNewsFeedForDisplay()
                List<FeedItemInfo> testFeedItemInfoList = new List<FeedItemInfo>();
                for (ConnectApi.FeedItem item : testFeed.items) {
                    testFeedItemInfoList.add(new FeedItemInfo(item));
                }
                System.assertEquals(FEED_ITEM_ID, testFeedItemInfoList[0].getFeedItemId());
                System.assertEquals(SCOREBOARD_GROUP_ID, testFeedItemInfoList[0].getFeedItemParentId());
                System.assertEquals(SCOREBOARD_GROUP_NM, testFeedItemInfoList[0].getFeedItemParentName());
                System.assertEquals('http://www.smallicon.com/icon.jpg', testFeedItemInfoList[0].getFeedItemParentSmallIconUrl());
                System.assertEquals(LIKE_ID, testFeedItemInfoList[0].getMyLikeId());
                System.assertEquals('Just now.', testFeedItemInfoList[0].getRelativeCreatedDate());
                System.assertEquals('You like this.', testFeedItemInfoList[0].getLikesMessage());
                System.assertEquals('http://www.theRenditionUrl.com', testFeedItemInfoList[0].getImageUrl());
                System.assertEquals('theAttachmentDescription', testFeedItemInfoList[0].getContentDescription());
                System.assertEquals('theAttachmentTitle', testFeedItemInfoList[0].getContentTitle());
                System.assertEquals(false, testFeedItemInfoList[0].getHasImagePreview());
                System.assertEquals('1.0', testFeedItemInfoList[0].getContentId());
                System.assertEquals(CONTENT_DWNLD_URL, testFeedItemInfoList[0].getContentDownloadUrl());
            Test.stopTest();
        }
}

Any help or insight into this issue is greatly appreciated.

1 Answer 1

5

As it turns out, I overlooked something very simple. When calling the setTest() methods you need to make sure that variables maintain their state once set and therefore, because I was not declaring my variables as static, they were not maintaining their state and thus the error was being thrown.

Here's the modified controller code:

public class CanNewsChatterFeedController {
    //declare necessary static variables
    static ConnectApi.ChatterGroupPage chatterGrpPg;
    static String theGroupId;

    //empty constructor
    public CanNewsChatterFeedController(){}

    // get first page of news feed
    public static ConnectApi.FeedItemPage getNewsFeed() {
        //get the proper Chatter Group Page
        chatterGrpPg = getCanNewsChatterGrpPg();
       //get the groupId
       theGroupId = chatterGrpPg.groups[0].id;
       //use the groupId to get the feedItems
       ConnectApi.FeedItemPage feedPage = ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.Record, theGroupId);
       return feedPage;
    }

    //The Id of the ChatterGroup we want to display will be different in the sandbox from what it is in production
    //(production id: 0F9D0000000UGqS in sandbox id:0F9c0000000Chu1), in order to get the feed items we want, 
    //we have to search for the ChatterGroup by name. Since Group Name must be unique,
    //the searchGroups method should only return one ChatterGroupPage with one ChatterGroup
    public static ConnectApi.ChatterGroupPage getCanNewsChatterGrpPg(){
        return ConnectApi.ChatterGroups.searchGroups(null, 'Canada - Corby Scoreboard', null, 1);
    }    

    // build list of wrapped feed items for display in VisualForce
    public static 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;
    }


    public PageReference refreshFeed(){
        return null;
    }
}

I hope this helps someone else experiencing the same issue.

You must log in to answer this question.

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