• Chris Stark 3
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 1
    Questions
  • 1
    Replies
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.
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.

We have run into an issue when running unit tests that call a class from a managed package using a mock. When doing data manipulation within a test method (inserting new records) calling the class returns null value (in the case below a string 'Success' is expected to return).

@isTest(SeeAllData=true)
	static void testMock1() {
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
	}


Running the test above, the debug of 'Success' returns as expected. However, if any data manipulation is done at the beginning of the test method, null is always returned.
 

@isTest(SeeAllData=true)
	static void testMock1() {
		Account a = new Account();
		insert a;
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
	}

The bahavior completely changes, and the debug returns null. We have found a way around it doing the following.

@isTest(SeeAllData=true)
	static void testMock1() {
		Account a = new Account();
		insert a;
		test.starttest();
		system.debug(ManagedPackage1.Class1.ReturnSuccess('result', new ManagedPackage1.MockGenerator.Result1());
		test.stoptest();
	}
The result returns 'Success' as intended wrapped in test.starttest() and test.stoptest(). If the insert of the Account is also done within the start and stop test, the debug is again null. Any ideas as to why this behaves the way it does?