+ Start a Discussion

Exception - Posting to chatter gives an exception

I am posting to chatter and I get below while a test runs and tries to execute the chatter apex code. 
caused by: system.UnsupportedOperationException: ConnectApi methods are not supported in data siloed tests. Please use @IsTest(SeeAllData=true).

Has anyone faced this before. Thanks 

I have not ever worked upon the chatter APIs but by looking at this exception I can deduce that you will have to use @isTest(Seealldata=true) to the test classes where these chatter api are being used, since we cannot create chatter api related records in test class, we have to use organization data instead. To use organization data, we should place seealldata=true at the beginning of test class to resolve the issues.
Thanks for the response. I was aware of this solution but in my situation I cannot use it because  I have other tests  that start failing when I specify seeAllData=true as assert counts start failing for various tests which exist. So I wondering if there is some other solution to this
Your test case will have to have the @seeAllData=true annotation - its a restriction on the API usage. You don't have to do this for all tests though, you can just annotate a single test method that uses the API. There is no other solution - if you have other asserts within the same test that start failing, that implies you don't have a unit test, rather an integration test that covers a number of scenarios. Your only option here is to refactor the tests.
Thanks Bob. Really appreciate your response. Refactor tests is a good option and right approach. But since there are lot of tests and this needs to be submitted ASAP i went with a shorter route. I found a faster workaround for now. I encapsulated the chatter api code within (!Test.isRunningTest()) so that chatter code gets skipped when its called within other tests.So now all existing tests pass.