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
SFDC Apex DevSFDC Apex Dev 

I have written the test class for the following apex class but not able to cover much code ... help me out to write the test class.....

APEX CLASS

public with sharing class GrpsBtnController {
    
    private static final String PvtGrp = 'Private';
    public static final String GstSrTyp = 'Guest';

    @AuraEnabled
    public static GrpBtnWrapper getGroupMemberInfo(String groupId) {
        
        GrpBtnWrapper response = new GrpBtnWrapper();
        List<CollabGrp> groupList = new List<CollabGrp>();
        List<CollabGrpM> GrpMem = new List<CollabGrpM>();

        if(String.isNotBlank(groupId)) {
            response.baseURL = PCGCUtility.returnSiteBaseURL();

            User usr = [SELECT Id,UserType FROM User WHERE Id =:UserInfo.getUserID() LIMIT 1];
            response.userObj = usr;

            try {
                groupList = [SELECT Id,ColTyp,OwnerId FROM CollabGrp WHERE
                             Id =:groupId LIMIT 1];

                if(!groupList.isEmpty() && groupList[0].OwnerId != UserInfo.getUserId() 
                    && PvtGrp.equalsIgnoreCase(groupList[0].ColTyp)) {

                    GrpMem = [SELECT Id,CollabGrpId,MemberId FROM 
                                       CollabGrpM WHERE MemberId =:UserInfo.getUserId()
                                       AND CollabGrpId =:groupId LIMIT 1];
                    
                    if(!GrpMem.isEmpty()) {
                        response.showGrpMemD = true;
                    }
                    else {
                        response.showGrpMemD = false;

                    }
                }

                else {
                    response.showGrpMemD = true;
                }
            }
            catch(Exception ex) {
                system.debug('---Exception---'+ex.getMessage());
            }
        }
        else {
            response.showGrpMemD = false;

        }
        return response;
    }
}

TEST CLASS

@isTest
public with sharing class GrpsBtnControllerTest {
    
    private static User AnUser;
    private static Account acc;
    public static CollabGrp newGroup;

    @testSetup static void createTestData() {
          
        acc = PCTDUtility.createAccount('Test Account 3');
        insert acc;
        
        System.assertNotEquals(null,acc);
        
        Contact con = PCTDUtility.createPtContact('Contact 3','Test',acc.Id);
        insert con;
        
        System.assertNotEquals(null,con);
        li360User = PCTDUtility.createPtStUser('Test731','ShippingAndMailing871',
                           'TUABC36','testPtUser2@testorg.com','testPtUser79@testorg.com.pb','TestShippingAndMailing1393',
                           con.Id);

    }

    private static testMethod void getPubGrpMInfoTest() {
        
        User li360User = [SELECT Id FROM User WHERE communityNickName = 'TestShippingAndMailing1393' LIMIT 1];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 2';
        newGroup.ColTyp='Public';
        newGroup.OwnerId = UserInfo.getUserId();
        insert newGroup;
        
        Test.startTest();
            grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }

    private static testMethod void getPvtGrpMemInfoTest() {
        
        User li360User = [SELECT Id FROM User WHERE communityNickName = 'TestShippingAndMailing1393' LIMIT 1];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = UserInfo.getUserId();
        insert newGroup;
        
        Test.startTest();
            grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMemD);
    }
}

the bold and italic part is not able to cover........ Could anyone help me out....
Rambo SusonRambo Suson
hi,
CollabGrp  object is null, so  you need create some fake data and  insert to this object. so that you can execute the rest code.
SFDC Apex DevSFDC Apex Dev
here this is the dummy data created in collabgrp object......

newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = UserInfo.getUserId();
        insert newGroup;


still it is not covering that part Rambo
Niraj Kr SinghNiraj Kr Singh
Hi Allen2,

Update your @testSetup Method with this below code: (Where creating two users)
@testSetup static void createTestData() {
          
        acc = PCTDUtility.createAccount('Test Account 3');
        insert acc;
        
        System.assertNotEquals(null,acc);
        
        List<Contact> con = new List<Contact>();
		con.add(PCTDUtility.createPtContact('Contact 3','Test',acc.Id));
        insert con;
        
        System.assertNotEquals(null,con);
        List<User> lstUser = new List<User>();
		lstUser.add(PCTDUtility.createPtStUser('Test731','ShippingAndMailing871',
                           'TUABC36','testPtUser2@testorg.com','testPtUser79@testorg.com.pb','TestShippingAndMailing1393',
                           con[0].Id));
		lstUser.add(PCTDUtility.createPtStUser('Test732','ShippingAndMailing872',
                           'TUABC37','testPtUser3@testorg.com','testPtUser78@testorg.com.pb','TestShippingAndMailing1394',
                           con[1].Id));
						   
    }

And add following these test methods to cover your other If / Else part:
private static testMethod void getPvtGrpMemInfoIFTest() {
        
        List<User> lstUser = [SELECT Id FROM User WHERE communityNickName = 'TestShippingAndMailing1393' LIMIT 1];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
		
		CollabGrpM objGrpM = new CollabGrpM();
		objGrpM.CollabGrpId = newGroup.Id;
		objGrpM.MemberId = lstUser[1].Id;
		//Add other required field here for this object
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }
	
	private static testMethod void getPvtGrpMemInfoELSETest() {
        
        List<User> lstUser = [SELECT Id FROM User WHERE communityNickName = 'TestShippingAndMailing1393' LIMIT 1];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
		
		CollabGrpM objGrpM = new CollabGrpM();
		objGrpM.CollabGrpId = newGroup.Id;
		objGrpM.MemberId = lstUser[0].Id;
		//Add other required field here for this object
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }

	private static testMethod void getPubGrpMInfoNullTest() {
        
        List<User> lstUser = [SELECT Id FROM User WHERE communityNickName = 'TestShippingAndMailing1393' LIMIT 1];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 2';
        newGroup.ColTyp='Public';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(null); //It will cover else part of if(String.isNotBlank(groupId))
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }
Might be some update needed, Check it

Hope it will work for you

Thanks
Niraj
SFDC Apex DevSFDC Apex Dev
Hi Niraj,
getting an error  "System.ListException: List index out of bounds: 1" at line 19
Niraj Kr SinghNiraj Kr Singh
Oops... I forget to remove the limit and Where condition from SOQL happening in each method.
Plz take this code once again:

@testSetup method:
@testSetup static void createTestData() {
          
        acc = PCTDUtility.createAccount('Test Account 3');
        insert acc;
        
        System.assertNotEquals(null,acc);
        
        List<Contact> con = new List<Contact>();
		con.add(PCTDUtility.createPtContact('Contact 3','Test',acc.Id));
                con.add(PCTDUtility.createPtContact('Contact 4','Test1',acc.Id));
        insert con;
        
        System.assertNotEquals(null,con);
        List<User> lstUser = new List<User>();
		lstUser.add(PCTDUtility.createPtStUser('Test731','ShippingAndMailing871',
                           'TUABC36','testPtUser2@testorg.com','testPtUser79@testorg.com.pb','TestShippingAndMailing1393',
                           con[0].Id));
		lstUser.add(PCTDUtility.createPtStUser('Test732','ShippingAndMailing872',
                           'TUABC37','testPtUser3@testorg.com','testPtUser78@testorg.com.pb','TestShippingAndMailing1394',
                           con[1].Id));
						   
    }
Test Methods:
private static testMethod void getPvtGrpMemInfoIFTest() {
        
        List<User> lstUser = [SELECT Id FROM User]; 
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
		
		CollabGrpM objGrpM = new CollabGrpM();
		objGrpM.CollabGrpId = newGroup.Id;
		objGrpM.MemberId = lstUser[1].Id;
		//Add other required field here for this object
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }
	
	private static testMethod void getPvtGrpMemInfoELSETest() {
        
        List<User> lstUser = [SELECT Id FROM User];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 1';
        newGroup.ColTyp='Private';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
		
		CollabGrpM objGrpM = new CollabGrpM();
		objGrpM.CollabGrpId = newGroup.Id;
		objGrpM.MemberId = lstUser[0].Id;
		//Add other required field here for this object
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(newGroup.Id);
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }

	private static testMethod void getPubGrpMInfoNullTest() {
        
        List<User> lstUser = [SELECT Id FROM User];
        GrpBtnWrapper grpWrapper = new GrpBtnWrapper();
        
        newGroup = new CollabGrp();
        newGroup.Name='Chatter Group 2';
        newGroup.ColTyp='Public';
        newGroup.OwnerId = lstUser[0].Id;
        insert newGroup;
        
        Test.startTest();
			System.runAs(lstUser[1]) {
				grpWrapper = GrpsBtnController.getGrpMemInfo(null); //It will cover else part of if(String.isNotBlank(groupId))
			}
        Test.stopTest();
        system.assertEquals(true,grpWrapper.showGrpMembD);
    }
Try once...

 
SFDC Apex DevSFDC Apex Dev
I tried it already but the error is the same and on the same line 19