Polishing
This commit is contained in:
@@ -109,7 +109,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
||||
|
||||
if (!(beanFactory instanceof BeanDefinitionRegistry registry)) {
|
||||
throw new IllegalStateException("Cannot process bean override with a BeanFactory " +
|
||||
"that doesn't implement BeanDefinitionRegistry: " + beanFactory.getClass());
|
||||
"that doesn't implement BeanDefinitionRegistry: " + beanFactory.getClass().getName());
|
||||
}
|
||||
|
||||
// The following is a "pseudo" bean definition which MUST NOT be used to
|
||||
@@ -176,10 +176,12 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
||||
int candidateCount = candidateNames.size();
|
||||
if (candidateCount != 1) {
|
||||
Field field = overrideMetadata.getField();
|
||||
throw new IllegalStateException("Unable to select a bean to override by wrapping: found " +
|
||||
candidateCount + " bean instances of type " + overrideMetadata.getBeanType() +
|
||||
" (as required by annotated field '" + field.getDeclaringClass().getSimpleName() +
|
||||
"." + field.getName() + "')" + (candidateCount > 0 ? ": " + candidateNames : ""));
|
||||
throw new IllegalStateException("""
|
||||
Unable to select a bean to override by wrapping: found %d bean instances of type %s \
|
||||
(as required by annotated field '%s.%s')%s"""
|
||||
.formatted(candidateCount, overrideMetadata.getBeanType(),
|
||||
field.getDeclaringClass().getSimpleName(), field.getName(),
|
||||
(candidateCount > 0 ? ": " + candidateNames : "")));
|
||||
}
|
||||
beanName = BeanFactoryUtils.transformedBeanName(candidateNames.iterator().next());
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ class BeanOverrideContextCustomizer implements ContextCustomizer {
|
||||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
if (!(context instanceof BeanDefinitionRegistry registry)) {
|
||||
throw new IllegalStateException("Cannot process bean overrides with an ApplicationContext " +
|
||||
"that doesn't implement BeanDefinitionRegistry: " + context.getClass());
|
||||
"that doesn't implement BeanDefinitionRegistry: " + context.getClass().getName());
|
||||
}
|
||||
registerInfrastructure(registry);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class BeanOverrideRegistrar implements BeanFactoryAware {
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {
|
||||
throw new IllegalStateException("Cannot process bean override with a BeanFactory " +
|
||||
"that doesn't implement ConfigurableBeanFactory: " + beanFactory.getClass());
|
||||
"that doesn't implement ConfigurableBeanFactory: " + beanFactory.getClass().getName());
|
||||
}
|
||||
this.beanFactory = cbf;
|
||||
}
|
||||
|
||||
@@ -49,29 +49,28 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
reinjectFieldsIfConfigured(testContext);
|
||||
reinjectFieldsIfNecessary(testContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the test instance and make sure that fields flagged for bean
|
||||
* overriding are processed.
|
||||
* <p>Each field's value will be updated with the overridden bean instance.
|
||||
* overriding are injected with the overridden bean instance.
|
||||
*/
|
||||
protected void injectFields(TestContext testContext) {
|
||||
postProcessFields(testContext, (testMetadata, overrideRegistrar) -> overrideRegistrar.inject(
|
||||
testMetadata.testInstance, testMetadata.overrideMetadata));
|
||||
postProcessFields(testContext, (testMetadata, registrar) ->
|
||||
registrar.inject(testMetadata.testInstance, testMetadata.overrideMetadata));
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the test instance and make sure that fields flagged for bean
|
||||
* overriding are processed.
|
||||
* overriding are injected with the overridden bean instance, if necessary.
|
||||
* <p>If a fresh instance is required, the field is nulled out and then
|
||||
* re-injected with the overridden bean instance.
|
||||
* <p>This method does nothing if the
|
||||
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
|
||||
* attribute is not present in the {@code TestContext}.
|
||||
* attribute is not present in the {@code TestContext} with a value of {@link Boolean#TRUE}.
|
||||
*/
|
||||
protected void reinjectFieldsIfConfigured(TestContext testContext) throws Exception {
|
||||
protected void reinjectFieldsIfNecessary(TestContext testContext) throws Exception {
|
||||
if (Boolean.TRUE.equals(
|
||||
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user