AnnotationValidatorFactory { public AnnotationValidator createAnnotationValidator(ValidateWith validateWithAnnotation) { AnnotationValidator validator = VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation); if (validator != null) { return validator; } Class<? extends AnnotationValidator> clazz = validateWithAnnotation.value(); try { AnnotationValidator annotationValidator = clazz.newInstance(); VALIDATORS_FOR_ANNOTATION_TYPES.putIfAbsent(validateWithAnnotation, annotationValidator); return VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation); } catch (Exception e) { throw new RuntimeException("Exception received when creating AnnotationValidator class " + clazz.getName(), e); } }  AnnotationValidator createAnnotationValidator(ValidateWith validateWithAnnotation);  }
@Test public void createAnnotationValidator() { ValidateWith validateWith = SampleTestWithValidator.class.getAnnotation(ValidateWith.class); AnnotationValidator annotationValidator = new AnnotationValidatorFactory().createAnnotationValidator(validateWith); assertThat(annotationValidator, is(instanceOf(Validator.class))); }
@Test public void exceptionWhenAnnotationValidatorCantBeCreated() { ValidateWith validateWith = SampleTestWithValidatorThatThrowsException.class.getAnnotation(ValidateWith.class); exception.expect(RuntimeException.class); exception.expectMessage("Exception received when creating AnnotationValidator class " + "org.junit.validator.AnnotationValidatorFactoryTest$ValidatorThatThrowsException"); new AnnotationValidatorFactory().createAnnotationValidator(validateWith); }