KeyCloakUserManager extends BaseKeyCloakManager implements UserManager,
                                                                        ContextualManager { @Override public void assignGroups(String username, Collection<String> groups) throws SecurityManagementException { checkNotNull("username", username); final Set<String> userRoles = SecurityManagementUtils.rolesToString(SecurityManagementUtils.getRoles(userSystemManager, username)); userRoles.addAll(groups); assignGroupsOrRoles(username, userRoles); } KeyCloakUserManager(); @Override void initialize(final UserSystemManager userSystemManager); @Override SearchResponse<User> search(SearchRequest request); @Override User get(String username); @Override List<User> getAll(); @Override User create(User entity); @Override User update(User entity); @Override void delete(String... identifiers); @Override UserManagerSettings getSettings(); @Override void assignGroups(String username,
                             Collection<String> groups); @Override void assignRoles(String username,
                            Collection<String> roles); @Override void changePassword(String username,
                               String newPassword); @Override void destroy();  }
@Test public void testAssignGroups() { final User user = mock(User.class); when(user.getIdentifier()).thenReturn("user0"); when(user.getRoles()).thenReturn(new HashSet<Role>()); UserManager userManagerMock = mock(UserManager.class); doAnswer(new Answer<User>() { @Override public User answer(InvocationOnMock invocationOnMock) throws Throwable { return user; } }).when(userManagerMock).get("user0"); when(userSystemManager.users()).thenReturn(userManagerMock); final Collection<String> groups = new ArrayList<String>(); groups.add("role1"); groups.add("role2"); UserResource user0Resource = userResources.get(0); RoleMappingResource roleMappingResource = user0Resource.roles(); RoleScopeResource roleScopeResource = roleMappingResource.realmLevel(); usersManager.assignGroups("user0", groups); ArgumentCaptor<List> rolesCaptor = ArgumentCaptor.forClass(List.class); verify(roleScopeResource, times(1)).add(rolesCaptor.capture()); List rolesAdded = rolesCaptor.getValue(); assertEquals(2, rolesAdded.size()); }