GroupValidator implements EntityValidator<Group> { @Override public Set<ConstraintViolation<Group>> validate(Group entity) { if (entity == null) { return null; } final String id = entity.getName(); Set<ConstraintViolation<Group>> result = new HashSet<ConstraintViolation<Group>>(1); if (id == null || id.trim().length() == 0) { final String msg = getMessage(KEY_NAME_NOT_EMPTY); result.add(createViolation(entity, "name", msg)); } return result; }  @Override Set<ConstraintViolation<Group>> validate(Group entity); abstract String getMessage(final String key); static final String KEY_NAME_NOT_EMPTY; }
@Test public void testValid() { final Group group = mock(Group.class); when(group.getName()).thenReturn("group1"); Set<ConstraintViolation<Group>> violations = new GroupValidatorTestImpl().validate(group); assertTrue(violations.isEmpty()); }