You need to sign in to do that
Don't have an account?
buggs sfdc
Test class for GroupMember
HI
Can any one help me out in testing the same scenario for delete statement.
Really appreciate your quick responses.
Thanks
Can any one help me out in testing the same scenario for delete statement.
Really appreciate your quick responses.
Thanks
trigger userdeactive on User (before insert,before update) { List<GroupMember> group = new List<GroupMember>([select Id, UserOrGroupId, groupid From GroupMember where groupId IN (SELECT id FROM group WHERE Type = 'Queue') AND UserOrGroupId IN (SELECT id from user WHERE isactive=FALSE)]); delete group; }
@isTest private class deActiveUserTest { static testMethod void validateremoveInActiveUser() { { User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()]; System.runAs (thisUser) { // Insert user Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; User u = new User(Alias = 'standt', Email='test@test.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='sdss123@test.com'); insert u; // Insert group Group grp = new Group(); grp.Name = 'Technology Partner Content'; insert grp; test.starttest(); system.assertequals(u,[select id from user WHERE isactive=FALSE]); delete grp; test.stoptest(); // Insert contact with your required field. Contact cnt = new Contact(); cnt.LastName = 'Test contact'; cnt.Email = 'test@test.com'; //cnt.Public_Group_Technology_Partner_Content__c = true; insert cnt; } } } }
A great thanks for quick response,yes i need to set the Group.Type value to "Queue" and delete the inactive users in the group.as a newbie to apex code i just have few questions.
-Here its covering the the trigger 100% with the above test class,but i havent used any delete statment in my test class,so will it work if i push the same code to live.
-And some one suggested me to use system.Assert statements ,i googled it but still i havent got any perfect answer,can you clear me off exactly when we wanted to use system.assert statements.
If you are only seeking to remove the User that was just deactivated, you must use a for loop to iterate through all Users that were updated in the transaction to bulkify the trigger. In the for loop you would have a conditional that tests if the update to the User.IsActive field changed from True to False and only include those Users' IDs in the query to find their Group Member records for deletion.
There are lots of ways to use the Assert statements, but what you would most likely want in this unit test is to use at least two. First, before you deactivate the User, you want to do an assertion that the User is actually a member of the Queue Group you created in the test. And second, after the update to the User, you want to assert that it no longer is a member of the Queue Group.
I tend to be very obsessive about my unit tests and asserts and would probably create a second User in the Group and also do an assert to confirm that it remained in the Group after the update to deactivate the first User.