### Question:
ErrorDescriptorBuilderFinder { @SuppressWarnings("unchecked") public ValidationErrorDescriptorBuilder find(final ConstraintViolation violation) { ValidationErrorDescriptorBuilder validationErrorDescriptorBuilder = null; final ConstraintDescriptor constraintDescriptor = violation.getConstraintDescriptor(); final List<Class<ConstraintValidator>> constraintValidatorClasses = constraintDescriptor.getConstraintValidatorClasses(); if (constraintValidatorClasses != null) { for (Class<ConstraintValidator> constraintValidatorClass : constraintValidatorClasses) { if (ValidationErrorDescriptorBuilder.class.isAssignableFrom(constraintValidatorClass)) { try { final ConstraintValidator validator = constraintValidatorClass.newInstance(); validator.initialize(constraintDescriptor.getAnnotation()); validationErrorDescriptorBuilder = (ValidationErrorDescriptorBuilder) validator; break; } catch (Exception e) { throw new IllegalStateException("Failed to instantiate validator", e); } } } } if (validationErrorDescriptorBuilder == null) { validationErrorDescriptorBuilder = genericTypeProcessorRegistry. getTypeProcessor(constraintDescriptor.getAnnotation().getClass().getInterfaces()[0], ValidationErrorDescriptorBuilder.class, false); } if (validationErrorDescriptorBuilder == null) { validationErrorDescriptorBuilder = new DefaultConstraintValidationErrorDescriptorBuilder(); } return validationErrorDescriptorBuilder; }  @SuppressWarnings("unchecked") ValidationErrorDescriptorBuilder find(final ConstraintViolation violation);  }### Answer:
@Test public void find_violationContainsBuilder_returnInitializedBuilder() { ConstraintViolation violation = mockViolation(true, ConstraintValidatorMock.class); mockTypeProcessor(true); ValidationErrorDescriptorBuilder errorDescriptorBuilder = errorDescriptorBuilderFinder.find(violation); assertSame(errorDescriptorBuilder.getClass(), ConstraintValidatorMock.class); }
@Test(expected = IllegalStateException.class) public void find_violationContainsBuilderButInitIsFail_throwException() { ConstraintViolation violation = mockViolation(true, ConstraintValidatorMockWithoutDefaultConstructor.class); mockTypeProcessor(true); ValidationErrorDescriptorBuilder errorDescriptorBuilder = errorDescriptorBuilderFinder.find(violation); assertSame(errorDescriptorBuilder.getClass(), ConstraintValidatorMock.class); }
@Test @SuppressWarnings("unchecked") public void find_violationNotContainsBuilderButTypeProcessorCanFind_returnInitializedBuilder() { ConstraintViolation violation = mockViolation(false, ConstraintValidatorMock.class); ValidationErrorDescriptorBuilder builder = mockTypeProcessor(true); ValidationErrorDescriptorBuilder errorDescriptorBuilder = errorDescriptorBuilderFinder.find(violation); assertEquals(builder, errorDescriptorBuilder); }
@Test @SuppressWarnings("unchecked") public void find_violationAndTypeProcessorCannotFindBuilder_returnInitializedBuilder() { ConstraintViolation violation = mockViolation(false, ConstraintValidatorMock.class); mockTypeProcessor(false); ValidationErrorDescriptorBuilder errorDescriptorBuilder = errorDescriptorBuilderFinder.find(violation); assertSame(DefaultConstraintValidationErrorDescriptorBuilder.class, errorDescriptorBuilder.getClass()); }