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
Anonymous DeveloperAnonymous Developer 

Test Class Dilemma

public with sharing class AS_customTopbarNotification{
    
    @AuraEnabled
    public static Boolean getMessages(String communityId) {
        
        ConnectApi.FeedElementPage unread = ConnectApi.ChatterFeeds.getFeedElementsFromFeed(communityId, ConnectApi.FeedType.DirectMessages);
        Boolean hasUnread = false;
        
        for (ConnectApi.FeedElement feedEl : unread.elements) {
            if (!feedEl.capabilities.readBy.isReadByMe) {
                hasUnread = true;
                break;
            }
        }
        System.debug('hasUnread ' + hasUnread);
        return hasUnread;
    }
}


Can anyone make a test class out of this code?

Thanks in advance.
Best Answer chosen by Anonymous Developer
mukesh guptamukesh gupta
Hi,

Please follow below code:
 
@isTest
public class CanNewsChatterFeedTest{
    @isTest
    static void chatterFeedTest(){
	
	        Account a = [select name from Account where Id='0010Q00000DO8b7QAD'];
       // system.debug('Account = ' + a.Name);

        Contact c = New Contact(LastName = 'Test', AccountID = a.id);
        insert c;
       // system.debug('Contact = ' + c.Id);

        Profile p = [SELECT Id FROM Profile WHERE Name = 'Customer Community user' LIMIT 1];

        user communityUser = New User(
            UserName = 'test_' + math.random() + '@test.com',
            FirstName = 'Test-First',
            LastName = 'Test-Last',
            Alias = 'test',
            email = 'test' + math.random() + '@test.com',
            CommunityNickName = string.valueOf(math.random()).substring(0,6),
            ProfileID = p.id,
            TimeZoneSidKey = 'America/New_York', 
            LocaleSidKey = 'en_US', 
            EmailEncodingKey = 'UTF-8', 
            LanguageLocaleKey = 'en_US',
            ContactID = c.Id

            );


        insert communityUser;


	
	
	
        ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
        ConnectApi.FeedItem feedItem = new ConnectApi.FeedItem();
        ConnectApi.Comment comment = new ConnectApi.Comment();

        List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
        testItemList.add(feedItem);
        testPage.items = testItemList;

        ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(communityUser.Id, 'Hello');

   
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi,

Please follow below code:
 
@isTest
public class CanNewsChatterFeedTest{
    @isTest
    static void chatterFeedTest(){
	
	        Account a = [select name from Account where Id='0010Q00000DO8b7QAD'];
       // system.debug('Account = ' + a.Name);

        Contact c = New Contact(LastName = 'Test', AccountID = a.id);
        insert c;
       // system.debug('Contact = ' + c.Id);

        Profile p = [SELECT Id FROM Profile WHERE Name = 'Customer Community user' LIMIT 1];

        user communityUser = New User(
            UserName = 'test_' + math.random() + '@test.com',
            FirstName = 'Test-First',
            LastName = 'Test-Last',
            Alias = 'test',
            email = 'test' + math.random() + '@test.com',
            CommunityNickName = string.valueOf(math.random()).substring(0,6),
            ProfileID = p.id,
            TimeZoneSidKey = 'America/New_York', 
            LocaleSidKey = 'en_US', 
            EmailEncodingKey = 'UTF-8', 
            LanguageLocaleKey = 'en_US',
            ContactID = c.Id

            );


        insert communityUser;


	
	
	
        ConnectApi.FeedItemPage testPage = new ConnectApi.FeedItemPage();
        ConnectApi.FeedItem feedItem = new ConnectApi.FeedItem();
        ConnectApi.Comment comment = new ConnectApi.Comment();

        List<ConnectApi.FeedItem> testItemList = new List<ConnectApi.FeedItem>();
        testItemList.add(feedItem);
        testPage.items = testItemList;

        ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed(communityUser.Id, 'Hello');

   
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

If you face any specific issue while attempting do post that here.

Thanks!
Anonymous DeveloperAnonymous Developer
@Mukesh
Good day thank you for taking the time to write the code but sad to say it is not the code that I was expecting or wanted, nevertheless due to the time you put in on it ill just mark it as the best answer. What I was looking for was the code coverage in my class if you could be so kind to answer it would be much appreciated. Thanks in advance.

Here's my test class atm:

@isTest
public class AS_customNotificationTestClass {
    @testSetup static void testData() {
        String communityId = [SELECT Id FROM Network][0].Id;        
               
        ConnectApi.FeedElementPage testPage = new ConnectApi.FeedElementPage();
        List<ConnectApi.FeedItem> testEl = new List<ConnectApi.FeedItem>();
        ConnectApi.FeedItem feedItem1 = new ConnectApi.FeedItem();
        ConnectApi.FeedItem feedItem2 = new ConnectApi.FeedItem();
        
        ConnectApi.DirectMessageCapability dM1 = new ConnectApi.DirectMessageCapability();
        dM1.subject = 'Test 1';
        
        ConnectApi.ReadByCapability readby1 = new ConnectApi.ReadByCapability();
        readby1.isReadByMe = true;
        
        ConnectApi.FeedElementCapabilities capability1 = new ConnectApi.FeedElementCapabilities();
        capability1.readBy = readby1;
        capability1.directMessage = dM1;
        
        feedItem1.capabilities = capability1;
        
        ConnectApi.DirectMessageCapability dM2 = new ConnectApi.DirectMessageCapability();
        dM2.subject = 'Test 2';
        
        ConnectApi.ReadByCapability readby2 = new ConnectApi.ReadByCapability();
        readby2.isReadByMe = false;
        
        ConnectApi.FeedElementCapabilities capability2 = new ConnectApi.FeedElementCapabilities();
        capability2.readBy = readby2;
        capability2.directMessage = dM2;
        
        feedItem1.capabilities = capability2;
        
        testEl.add(feedItem1);
        testEl.add(feedItem2);
        testPage.elements = testEl;
        
        ConnectApi.ChatterFeeds.setTestGetFeedElementsFromFeed(null, ConnectApi.FeedType.DirectMessages, testPage);
    }
      
    @isTest
    static void doTest(){
        
        String communityId = [SELECT Id FROM Network][0].Id;
        
        Test.startTest();
        System.assertNotEquals(null, AS_customTopbarNotification.getInbox(null));
        Test.stopTest();
        
    }
}

I have no idea how to proceed I am stuck.
User-added image
 
Anonymous DeveloperAnonymous Developer
Never mind I got it.