function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris Stark 3Chris Stark 3 

Writing Tests for ConnectApi.getTopics(...)

Hello,

I am trying to write Unit Tests for the following code snippet:
 
// find topics matching the word "Cancer"
String query = 'Cancer';
ConnectApi.TopicPage page = ConnectApi.Topics.getTopics(Network.getNetworkId(), query, ConnectApi.topicSort.PopularDesc);
This code works fine when actually in use via my application or via Anonymous Apex, but fails to return any results in my unit test:
@isTest(SeeAllData=true)
	static void testTopicsWithMatches() {


		ConnectApi.CommunityPage cp = ConnectApi.Communities.getCommunities();

		if( cp == null || cp.communities == null || cp.communities.isEmpty() ) {
			System.assert( false, 'Unable to locate community!' );
		}

		ConnectApi.Community myCommunity = cp.communities.get( 0 );


		Test.startTest();

		ConnectApi.TopicPage topicPage = ConnectApi.Topics.getTopics( myCommunity.Id, 'Cancer', ConnectApi.topicSort.PopularDesc );
		System.debug( topicPage);
		System.debug( '-----' + topicPage.topics );

		Test.stopTest();
}
Even with the SeeAllData=true, the returned ConnectApi.TopicPage has no topics. Why is this? Do I need to set mock results for the ConnectApi somehow? If so, using which set method?

Any guidance is appreciated, thanks.
alouie_sfdcalouie_sfdc
As a first debugging step, try to hard-code your communityId in the call to ConnectApi.Topics.getTopics(). Perhaps the wrong one is getting used. You don't need to set mock results. There isn't a setTest method, and if there was one, you'd get an error when running the test.
Chris Stark 3Chris Stark 3
Thanks for the response. I updated the test as per your suggestion for debugging, although I believe it was properly picking up the community we want to use:
// create topic
System.debug( 'CREATING TOPIC' );
ConnectApi.Topic cancerTopic = ConnectApi.Topics.createTopic( '0DBg000000000TSGAY', 'Kingsguarding', '' );
System.debug( '...COMPLETE...' );

// query connectapi against the newly created topic
System.debug( 'QUERYING TOPIC' );
ConnectApi.TopicPage tp = ConnectApi.Topics.getTopics( '0DBg000000000TSGAY', 'Kingsguarding', ConnectApi.topicSort.PopularDesc );
System.debug( '...COMPLETE...' );

// do we have any results?
System.debug( 'Topic Page: ' + tp );
System.debug( 'Retrieved Topics: ' + tp.topics );
The debug log from this test execution is as follows:
08:26:47.3 (3441101)|EXECUTION_STARTED
08:26:47.3 (3446870)|CODE_UNIT_STARTED|[EXTERNAL]|01pg0000000BVCG|TEST_RESTGenericSearchController.testSearchTopicsWithMatches
08:26:47.3 (25135413)|USER_DEBUG|[619]|DEBUG|CREATING TOPIC
08:26:47.3 (72322856)|USER_DEBUG|[621]|DEBUG|...COMPLETE...
08:26:47.3 (72346783)|USER_DEBUG|[624]|DEBUG|QUERYING TOPIC
08:26:47.3 (89582732)|USER_DEBUG|[626]|DEBUG|...COMPLETE...
08:26:47.3 (90298360)|USER_DEBUG|[629]|DEBUG|Topic Page: ConnectApi.TopicPage[buildVersion=36.0, currentPageUrl=/services/data/v36.0/connect/communities/0DBg000000000TSGAY/topics?q=Kingsguarding&sort=PopularDesc, nextPageUrl=null, topics=()]
08:26:47.3 (90378349)|USER_DEBUG|[630]|DEBUG|Retrieved Topics: ()
Unfortunately I do not think that the problem was using the wrong community.