The users are created using the below code
String username = "john";
UserRepresentation user = new UserRepresentation();
LOGGER.info(String.format("Creating User %s", username));
user.setUsername(username);
// Enabling user by default
user.setEnabled(true);
// user.setAttributes(getUserAttributes());
user.setCreatedTimestamp(System.currentTimeMillis());
user.setCredentials(getCredentials("password"));
user.setEmail("john@smith.com");
// Setting email verification False by default
user.setEmailVerified(false);
user.setFirstName("John");
user.setLastName("Smith");
user.setGroups(Arrays.asList(new String[]{"users"}));
user.setRealmRoles(Arrays.asList(new String[] {"admin", "superadmin"}));
UsersResource usersResource = keycloak.realm(realm).users();
Response response = usersResource.create(user);
Groups and every other attribute are correctly set for the user created in Keycloak. But the realm roles, “admin” and “superadmin” are not associated with the created user. Please note that the roles and groups exist in keycloak before running the above code