You need to sign in to do that
Don't have an account?
SFDC 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....
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....
CollabGrp object is null, so you need create some fake data and insert to this object. so that you can execute the rest code.
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
Update your @testSetup Method with this below code: (Where creating two users)
And add following these test methods to cover your other If / Else part: Might be some update needed, Check it
Hope it will work for you
Thanks
Niraj
getting an error "System.ListException: List index out of bounds: 1" at line 19
Plz take this code once again:
@testSetup method: Test Methods: Try once...