UserGenerator implements CredentialGenerator<UserCredentialValue> { @Override public UserCredentialValue generateCredential(final GenerationParameters p) { final StringGenerationParameters params = (StringGenerationParameters) p; String username = params.getUsername(); if (username == null) { username = usernameGenerator.generateCredential().getStringCredential(); } final String password = passwordGenerator.generateCredential(params).getStringCredential(); return new UserCredentialValue(username, password, cryptSaltFactory.generateSalt(password)); } @Autowired  UserGenerator(
    final UsernameGenerator usernameGenerator,
    final PasswordCredentialGenerator passwordGenerator,
    final CryptSaltFactory cryptSaltFactory
  ); @Override UserCredentialValue generateCredential(final GenerationParameters p);  }
@Test public void generateCredential_givenAUsernameAndPasswordParameters_generatesUserWithUsernameAndGeneratedPassword() { passwordParameters.setUsername("test-user"); final UserCredentialValue user = subject.generateCredential(passwordParameters); assertThat(user.getUsername(), equalTo("test-user")); assertThat(user.getPassword(), equalTo("fake-generated-password")); assertThat(user.getSalt(), equalTo("fake-generated-salt")); }
@Test public void generateCredential_givenNoUsernameAndPasswordParameters_generatesUserWithGeneratedUsernameAndPassword() { final UserCredentialValue user = subject.generateCredential(passwordParameters); assertThat(user.getUsername(), equalTo("fake-generated-username")); assertThat(user.getPassword(), equalTo("fake-generated-password")); assertThat(user.getSalt(), equalTo("fake-generated-salt")); }