Polish bean override support in the TestContext framework

This commit is contained in:
Sam Brannen 2024-03-19 12:41:06 +01:00
parent 871860798e
commit 3bd342ee7d
6 changed files with 11 additions and 13 deletions

View File

@ -272,7 +272,7 @@ public class BeanOverrideBeanPostProcessor implements InstantiationAwareBeanPost
private void inject(Field field, Object target, String beanName) {
try {
field.setAccessible(true);
ReflectionUtils.makeAccessible(field);
Object existingValue = ReflectionUtils.getField(field, target);
Object bean = this.beanFactory.getBean(beanName, field.getType());
if (existingValue == bean) {

View File

@ -82,10 +82,8 @@ class BeanOverrideParser {
if (hasBeanOverride.get()) {
return;
}
long count = MergedAnnotations.from(field, DIRECT)
.stream(BeanOverride.class)
.count();
hasBeanOverride.compareAndSet(false, count > 0L);
boolean present = MergedAnnotations.from(field, DIRECT).isPresent(BeanOverride.class);
hasBeanOverride.compareAndSet(false, present);
});
return hasBeanOverride.get();
}
@ -94,7 +92,7 @@ class BeanOverrideParser {
AtomicBoolean overrideAnnotationFound = new AtomicBoolean();
MergedAnnotations.from(field, DIRECT).stream(BeanOverride.class).forEach(mergedAnnotation -> {
Assert.isTrue(mergedAnnotation.isMetaPresent(), "@BeanOverride annotation must be meta-present");
Assert.state(mergedAnnotation.isMetaPresent(), "@BeanOverride annotation must be meta-present");
BeanOverride beanOverride = mergedAnnotation.synthesize();
BeanOverrideProcessor processor = BeanUtils.instantiateClass(beanOverride.value());

View File

@ -28,7 +28,7 @@ import org.springframework.util.ReflectionUtils;
* {@code TestExecutionListener} that enables Bean Override support in tests,
* injecting overridden beans in appropriate fields of the test instance.
*
* <p>Some flavors of Bean Override might additionally require the use of
* <p>Some Bean Override implementations might additionally require the use of
* additional listeners, which should be mentioned in the javadoc for the
* corresponding annotations.
*
@ -62,7 +62,7 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
*/
protected void injectFields(TestContext testContext) {
postProcessFields(testContext, (testMetadata, postProcessor) -> postProcessor.inject(
testMetadata.overrideMetadata.field(), testMetadata.testInstance(), testMetadata.overrideMetadata()));
testMetadata.overrideMetadata.field(), testMetadata.testInstance, testMetadata.overrideMetadata));
}
/**
@ -73,7 +73,7 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
* attribute is not present in the {@code TestContext}.
*/
protected void reinjectFieldsIfConfigured(final TestContext testContext) throws Exception {
protected void reinjectFieldsIfConfigured(TestContext testContext) throws Exception {
if (Boolean.TRUE.equals(
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {

View File

@ -89,12 +89,12 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
@Override
public OverrideMetadata createMetadata(Field field, Annotation overrideAnnotation, ResolvableType typeToOverride) {
Class<?> declaringClass = field.getDeclaringClass();
// If we can, get an explicit method name right away; fail fast if it doesn't match.
if (overrideAnnotation instanceof TestBean testBeanAnnotation) {
Method overrideMethod = null;
String beanName = null;
if (!testBeanAnnotation.methodName().isBlank()) {
Class<?> declaringClass = field.getDeclaringClass();
overrideMethod = findTestBeanFactoryMethod(declaringClass, field.getType(), testBeanAnnotation.methodName());
}
if (!testBeanAnnotation.name().isBlank()) {
@ -134,7 +134,7 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
@Override
public String getBeanOverrideDescription() {
return "method convention";
return "@TestBean";
}
@Override

View File

@ -71,7 +71,7 @@ class MockDefinition extends Definition {
@Override
public String getBeanOverrideDescription() {
return "mock";
return "@MockitoBean";
}
@Override

View File

@ -61,7 +61,7 @@ class SpyDefinition extends Definition {
@Override
public String getBeanOverrideDescription() {
return "spy";
return "@MockitoSpyBean";
}
@Override