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) { private void inject(Field field, Object target, String beanName) {
try { try {
field.setAccessible(true); ReflectionUtils.makeAccessible(field);
Object existingValue = ReflectionUtils.getField(field, target); Object existingValue = ReflectionUtils.getField(field, target);
Object bean = this.beanFactory.getBean(beanName, field.getType()); Object bean = this.beanFactory.getBean(beanName, field.getType());
if (existingValue == bean) { if (existingValue == bean) {

View File

@@ -82,10 +82,8 @@ class BeanOverrideParser {
if (hasBeanOverride.get()) { if (hasBeanOverride.get()) {
return; return;
} }
long count = MergedAnnotations.from(field, DIRECT) boolean present = MergedAnnotations.from(field, DIRECT).isPresent(BeanOverride.class);
.stream(BeanOverride.class) hasBeanOverride.compareAndSet(false, present);
.count();
hasBeanOverride.compareAndSet(false, count > 0L);
}); });
return hasBeanOverride.get(); return hasBeanOverride.get();
} }
@@ -94,7 +92,7 @@ class BeanOverrideParser {
AtomicBoolean overrideAnnotationFound = new AtomicBoolean(); AtomicBoolean overrideAnnotationFound = new AtomicBoolean();
MergedAnnotations.from(field, DIRECT).stream(BeanOverride.class).forEach(mergedAnnotation -> { 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(); BeanOverride beanOverride = mergedAnnotation.synthesize();
BeanOverrideProcessor processor = BeanUtils.instantiateClass(beanOverride.value()); 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, * {@code TestExecutionListener} that enables Bean Override support in tests,
* injecting overridden beans in appropriate fields of the test instance. * 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 * additional listeners, which should be mentioned in the javadoc for the
* corresponding annotations. * corresponding annotations.
* *
@@ -62,7 +62,7 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
*/ */
protected void injectFields(TestContext testContext) { protected void injectFields(TestContext testContext) {
postProcessFields(testContext, (testMetadata, postProcessor) -> postProcessor.inject( 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} * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
* attribute is not present in the {@code TestContext}. * 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( if (Boolean.TRUE.equals(
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {

View File

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

View File

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

View File

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