+ Start a Discussion
Koustubh KulkarniKoustubh Kulkarni 

Need help to write test class?

Hi I am new to salesforce. i need help for writting a test class for the following controller.Can anyone help me with it.
public class my_groups
{
 Set<Id> setGroupMemberId = new Set<Id>();
    public cntrl_my_groups()
                        {
                       
                        List<CollaborationGroup> lstGroup = new List<CollaborationGroup>();
                        Id currentUserId = UserInfo.getUserId();
                        List<CollaborationGroupMember> lstGroupMember = [SELECT CollaborationGroupId,MemberId FROM CollaborationGroupMember where MemberId =:currentUserId];
                        for(CollaborationGroupMember gm : lstGroupMember)
                            {
                        setGroupMemberId.add(gm.CollaborationGroupId);   
                            }
                        }
    public list<CollaborationGroup> get_my_group
        {
            get
            {
            if(setGroupMemberId.size() > 0)
            get_my_group = [SELECT Id,Name FROM CollaborationGroup where Id in: setGroupMemberId];  
            return get_my_group;
            }
            set;
        }               
}

Any kind of help will be Appreciated.Thanks
Best Answer chosen by Koustubh Kulkarni
Abhishek BansalAbhishek Bansal
Hi,

Please find your required test class below :
@isTest 
private class my_groupsTestClass {
    static testMethod void testMethod1() {
    	CollaborationGroup cGroup = new CollaborationGroup();
    	cGroup.Name = 'Test Group'
    	insert cGroup;
    	
    	CollaborationGroupMember cGroupMember = new CollaborationGroupMember();
    	cGroupMember.CollaborationGroupId = cGroup.id;
    	cGroupMember.MemberId = UserInfo.getUserId();
    	insert cGroupMember;
    	
    	Test.startTest();
    	my_groups testController = new my_groups();
    	list<CollaborationGroup> testList = testController.get_my_group;
    	Test.stopTest();
    }
}
// Please insert all the required fields in CollaborationGroup and Collaborati
 
Please fill all the  required fields if any error appears for Required fields missing.
Let me know if there is any other issue.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please find your required test class below :
@isTest 
private class my_groupsTestClass {
    static testMethod void testMethod1() {
    	CollaborationGroup cGroup = new CollaborationGroup();
    	cGroup.Name = 'Test Group'
    	insert cGroup;
    	
    	CollaborationGroupMember cGroupMember = new CollaborationGroupMember();
    	cGroupMember.CollaborationGroupId = cGroup.id;
    	cGroupMember.MemberId = UserInfo.getUserId();
    	insert cGroupMember;
    	
    	Test.startTest();
    	my_groups testController = new my_groups();
    	list<CollaborationGroup> testList = testController.get_my_group;
    	Test.stopTest();
    }
}
// Please insert all the required fields in CollaborationGroup and Collaborati
 
Please fill all the  required fields if any error appears for Required fields missing.
Let me know if there is any other issue.

Thanks,
Abhishek
This was selected as the best answer
Koustubh KulkarniKoustubh Kulkarni
Thanks Abhishek for your help.But im getting a error saying DUPLICATE_VALUE, User is already a Member of this group.: [Member Id]. Can you help me with this?
Thanks
Koustubh
Koustubh KulkarniKoustubh Kulkarni
No worries, i resolved the issue. Thank you very much again Abhishek for the help you provided me on this.