I'm stuck trying to write a test method for the ConnectApi. Specifically, I have a remote action that uses the ConnectApi.QuestionAndAnswers.getSuggestions() method, which returns related (Chatter) questions and knowledge articles based on a text string. In order to test this remote action method, I have to set the related questions and/or articles in my test. Here is what I have so far:
@isTest
static void testFetchRelatedSuggestions() {
Network pub = [SELECT id FROM Network WHERE Name LIKE '%Power of Us%' LIMIT 1];
ConnectApi.QuestionAndAnswersSuggestions suggestions = new ConnectApi.QuestionAndAnswersSuggestions();
ConnectApi.ArticleItem article = new ConnectApi.ArticleItem();
suggestions.articles = new List<ConnectApi.ArticleItem>();
article.id = '000000000000123456';
article.rating = 3;
article.title = 'How to write a test in the ConnectAPi';
article.urlLink = 'http://foo.com';
article.viewCount = 33;
suggestions.articles.add(article);
ConnectApi.GenericFeedElement question = new ConnectApi.GenericFeedElement();
suggestions.questions = new List<ConnectApi.GenericFeedElement>();
question.body = new ConnectApi.FeedBody();
question.body.text = 'How do I use this Api?';
question.capabilities.questionAndAnswers.questionTitle = 'ConnectApi question';
question.createdDate = System.today();
question.feedElementType = ConnectApi.FeedElementType.FeedItem;
question.header.text = 'This is a question';
question.id = '000000000000456789';
question.parent.id = 'OOD123456789AAABBB';
question.url = 'http://feedelementurl.com';
suggestions.questions.add(question);
ConnectApi.QuestionAndAnswers.setTestGetSuggestions(
pub.id,
'How do I write a test for the ConnectApi?',
null,
true,
null,
suggestions
);
List<QuestionActionController.RelatedContent> result = QuestionActionController.fetchRelatedContent('How do I write a test for the ConnectApi?');
system.assertEquals(2,result.size());
}
The code above compiles but when I go to run the test, I am receiving a null pointer exception on this line:
question.capabilities.questionAndAnswers.questionTitle = 'ConnectApi question';
I think I understand why, because I need to instantiate either the ConnectApi.QuestionAndAnswersCapability class or the ConnectApi.FeedElementCapabilities class or both. The problem is that I have tried to both and hit compile errors. Both of these:
question.capabilities.questionAndAnswers = new ConnectApi.QuestionAndAnswersCapability();
question.capabilities = new ConnectApi.FeedElementCapabilities();
Throw the same compile error, which is Invalid Type: ConnectApi...
So I can't instantiate the capabilities, and I'm getting a null pointer presumably because I haven't instantiated the object(s). Ideas?
Here is a quick link to the API docs, in case anyone knows or is willing to dig in: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_connectapi_output_feed_element.htm