UserValidator implements EntityValidator<User> { @Override public Set<ConstraintViolation<User>> validate(final User entity) { if (entity == null) { return null; } final String id = entity.getIdentifier(); Set<ConstraintViolation<User>> result = new HashSet<ConstraintViolation<User>>(1); if (id == null || id.trim().length() == 0) { final String msg = getMessage(KEY_NAME_NOT_EMPTY); result.add(createViolation(entity, "identifier", msg)); } return result; }  @Override Set<ConstraintViolation<User>> validate(final User entity); abstract String getMessage(final String key); static final String KEY_NAME_NOT_EMPTY; }
@Test public void testValid() { final User user = mock(User.class); when(user.getIdentifier()).thenReturn("user1"); Set<ConstraintViolation<User>> violations = new UserValidatorTestImpl().validate(user); assertTrue(violations.isEmpty()); }