### Question:
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(); if (clazz == null) { throw new IllegalArgumentException("Can't create validator, value is null in annotation " + validateWithAnnotation.getClass().getName()); } 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);  }### Answer:
@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 exceptionWhenAnnotationWithNullClassIsPassedIn() { exception.expect(IllegalArgumentException.class); exception.expectMessage("Can't create validator, value is null in " + "annotation org.junit.validator.AnnotationValidatorFactoryTest$ValidatorWithNullValue"); new AnnotationValidatorFactory().createAnnotationValidator(new ValidatorWithNullValue()); }
@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); }