ExistingEntityValidator implements Validator { @Override public boolean validate(Object instance, Annotation annotation, Object value) { if (value == null) { return true; } ExistingEntity ann = ExistingEntity.class.cast(annotation); String entityId = value.toString(); int maxCount = ann.maxCount(); int minCount = ann.minCount(); this.projectName = getProjectName(instance, ann.projectField()); Collection<?> entities = getEntitiesList(ann.value(), entityId); return entities.size() >= minCount && entities.size() <= maxCount; }  @Override boolean validate(Object instance, Annotation annotation, Object value); @Override Class<? extends Annotation> getAnnotationClass(); @Override String getMessage(Annotation annotation, String fieldName, Object value);  }
@Test public void testValidate() { Object instance = new TestInstance(); assertThat(validator.validate(instance, null, null), is(true)); assertThat(validator.validate( instance, createAnnotation(entity), id ), is(true)); assertThat(validator.validate( instance, createAnnotation(entity, 2, 3), id ), is(false)); }