2

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.​QuestionAndAnswers​Capability class or the ConnectApi.​FeedElement​Capabilities class or both. The problem is that I have tried to both and hit compile errors. Both of these:

question.capabilities.questionAndAnswers = new ConnectApi.​QuestionAndAnswers​Capability();
question.capabilities = new ConnectApi.​FeedElement​Capabilities();

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

  • In BritishBoyinDC's answer, this worked for him: question.capabilities = new ConnectApi.​FeedElement​Capabilities(); ... are you sure it was causing a compile error for you? – alouie Oct 3 '14 at 6:41
5

I had to comment some stuff out to make it work in my org, but the code below ran and passed. In my experience, you pretty much have to create an instance of any class you need before you can use it in a test, so you'll see me creating an instance of the capabilities class, and the actor class.

The one piece I am not sure on is the question.parent - I just created a blank instance of the userdetail class, though I see you are setting Parent = OrgId, not a user - so you might need to adjust that if you need that to be set for the test to work...

@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 = new ConnectAPI.FeedElementCapabilities();
    question.capabilities.questionAndAnswers = new ConnectAPI.QuestionAndAnswersCapability();
    question.capabilities.questionAndAnswers.questionTitle = 'ConnectApi question';
    ConnectAPI.MessageBody msgBody = new ConnectAPI.MessageBody();
    question.header =  msgBody;
    question.createdDate = System.today();
    question.feedElementType = ConnectApi.FeedElementType.FeedItem;
    question.id = '000000000000456789';
    ConnectAPI.ActorWithId parentrecord = new ConnectAPI.UserDetail ();
    //parentrecord.id = 'OOD123456789AAABBB';
    question.parent = parentrecord;
    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());
 }
| improve this answer | |
  • Got it working, thanks Peter! Odd, that I swear I tried to instantiate each class. – greenstork Oct 3 '14 at 14:51
  • 1
    This is really strange, but I just had the same problem, trying to instantiate a "Capability" class. It was giving me an Invalid Type error as well. I had two "identical" lines; one which would compile and one that wouldn't (the one that would was copied and adapted from @BritishBoyinDC 's answer...thanks!). I discovered there were some extra hidden characters inserted in the one line that wouldn't compile. I'm thinking it's a weird MavensMate bug that I have never seen before. – ArtieBrosius Nov 7 '14 at 15:46
  • 1
    And...I have to absolve my favorite IDE, MavensMate. The problem turned out to be my laziness. I copied the full class name from the Apex Dev Guide website and pasted it into my code. Apparently, what appears to be a period is not really a period when copied from the page. Hope that helps someone. – ArtieBrosius Nov 7 '14 at 16:14
  • Thank you for posting this! I was having the same trouble after copying & pasting a Capability example from the Salesforce.com docs. Retyping the line manually fixed it! – Rob Mar 24 '15 at 12:10

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.