### Question:
RegistrationValidator implements Validator { @Override public void validate(Object target, Errors errors) { if (target == null) { return; } Player player = (Player) target; String name = player.getReadableName(); if (!validateNicknameStructure(name)) { errors.rejectValue("readableName", "registration.nickname.invalid", new Object[]{ name }, null); } if (!checkNameUniqueness(name)) { errors.rejectValue("readableName", "registration.nickname.alreadyUsed", new Object[]{ name }, null); } String email = player.getEmail(); if (!validateEmailStructure(email)) { errors.rejectValue("email", "registration.email.invalid", new Object[]{ email }, null); } if (!checkEmailUniqueness(email)) { errors.rejectValue("email", "registration.email.alreadyUsed"); } String password = player.getPassword(); if (!checkPasswordNotEmpty(password)) { errors.rejectValue("password", "registration.password.empty"); } if (!checkPasswordLength(password)) { errors.rejectValue("password", "registration.password.length", new Object[] { minPasswordLen }, null); } if (!checkPasswordConfirmation(password, player.getPasswordConfirmation())) { errors.rejectValue("passwordConfirmation", "registration.password.invalidConfirmation"); } String gameName = rooms.getGameName(player.getGameName()); if (!validator.isGameName(gameName, CANT_BE_NULL)) { errors.rejectValue("gameName", "registration.game.invalid", new Object[]{ gameName }, null); } }  @Override boolean supports(Class<?> clazz); @Override void validate(Object target, Errors errors);  }### Answer:
@Test public void shouldPassValidUser() { validator.validate(player, errors); assertTrue("Valid player binding result must contain no errors", !errors.hasErrors()); }
@Test public void shouldValidateNicknameStructure() { player.setReadableName("Unreadablename"); validator.validate(player, errors); assertError(errors, "readableName", "registration.nickname.invalid"); }
@Test public void shouldValidateUsernameUniqueness() { String nonUniqueName = "Nonunique Name"; when(registration.nameIsUsed(nonUniqueName)).thenReturn(true); player.setReadableName(nonUniqueName); validator.validate(player, errors); assertError(errors,"readableName", "registration.nickname.alreadyUsed"); }
@Test public void shouldValidateEmailUniqueness() { String nonUniqueEmail = "duplicate@sample.org"; when(registration.emailIsUsed(nonUniqueEmail)).thenReturn(true); player.setEmail(nonUniqueEmail); validator.validate(player, errors); assertError(errors, "email", "registration.email.alreadyUsed"); }
@Test public void shouldRejectEmptyPassword() { player.setPassword(""); validator.validate(player, errors); assertError(errors, "password", "registration.password.empty"); }
@Test public void shouldRejectShortPassword() { player.setPassword("1234"); validator.validate(player, errors); assertError(errors, "password", "registration.password.length"); }
@Test public void shouldCheckPasswordConfirmation() { player.setPassword("12345"); player.setPasswordConfirmation("1234"); validator.validate(player, errors); assertError(errors,"passwordConfirmation", "registration.password.invalidConfirmation"); }
@Test public void shouldValidateGameName() { String invalidGameName = "invalidGame"; when(commonValidator.isGameName(invalidGameName, Validator.CANT_BE_NULL)).thenReturn(false); player.setGameName(invalidGameName); validator.validate(player, errors); assertError(errors, "gameName", "registration.game.invalid"); }