Skip to content

Commit

Permalink
Polish bean override support in the TestContext framework
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Mar 19, 2024
1 parent 8718607 commit 3bd342e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void inject(Field field, Object target, OverrideMetadata overrideMetadata) {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ boolean hasBeanOverride(Class<?> testClass) {
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();
}
Expand All @@ -94,7 +92,7 @@ private void parseField(Field field, Class<?> source) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* {@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.
*
Expand Down Expand Up @@ -62,7 +62,7 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
*/
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));
}

/**
Expand All @@ -73,7 +73,7 @@ protected void injectFields(TestContext testContext) {
* {@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))) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public static Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodRe

@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()) {
Expand Down Expand Up @@ -134,7 +134,7 @@ protected String getExpectedBeanName() {

@Override
public String getBeanOverrideDescription() {
return "method convention";
return "@TestBean";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MockDefinition extends Definition {

@Override
public String getBeanOverrideDescription() {
return "mock";
return "@MockitoBean";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SpyDefinition extends Definition {

@Override
public String getBeanOverrideDescription() {
return "spy";
return "@MockitoSpyBean";
}

@Override
Expand Down

0 comments on commit 3bd342e

Please sign in to comment.