### Question:
BeanConstraintValidatorFactory implements ConstraintValidatorFactory { @Override public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { try { String beanName = getBeanName(key); if (!beanFactory.isPrototype(beanName)) { String msg = "Bean [%s] must be of prototype scope."; throw new IllegalArgumentException(String.format(msg, beanName)); } return beanFactory.getBean(beanName, key); } catch (NoSuchElementException e) { return beanFactory.createBean(key); } } BeanConstraintValidatorFactory(ConfigurableListableBeanFactory beanFactory); @Override T getInstance(Class<T> key); @Override void releaseInstance(ConstraintValidator <?, ?> instance);  }### Answer:
@Test public void testGetInstance() { String[] names = {validatorBeanName}; when(beanFactory.getBeanNamesForType(validatorType)).thenReturn(names); when(beanFactory.isPrototype(validatorBeanName)).thenReturn(true); when(beanFactory.getBean(validatorBeanName, validatorType)).thenReturn(validator); TestConstraintValidator aValidator = factory.getInstance(validatorType); assertEquals(aValidator, validator); }
@Test(expected = IllegalArgumentException.class) public void testGetInstanceNotPrototype() { String[] names = {validatorBeanName}; when(beanFactory.getBeanNamesForType(validatorType)).thenReturn(names); when(beanFactory.isPrototype(validatorBeanName)).thenReturn(false); factory.getInstance(validatorType); }
@Test public void testGetInstanceNotInFactory() { String[] names = {}; when(beanFactory.getBeanNamesForType(validatorType)).thenReturn(names); when(beanFactory.createBean(validatorType)).thenReturn(validator); TestConstraintValidator aValidator = factory.getInstance(validatorType); assertEquals(aValidator, validator); }