Reject identical Bean Overrides
Prior to this commit, the Bean Override feature in the Spring TestContext Framework (for annotations such as @MockitoBean and @TestBean) silently allowed one bean override to override another "identical" bean override; however, Spring Boot's @MockBean and @SpyBean support preemptively rejects identical overrides and throws an IllegalStateException to signal the configuration error to the user. To align with the behavior of @MockBean and @SpyBean in Spring Boot, and to help developers avoid scenarios that are potentially confusing or difficult to debug, this commit rejects identical bean overrides in the Spring TestContext Framework. Note, however, that it is still possible for a bean override to override a logically equivalent bean override. For example, a @TestBean can override a @MockitoBean, and vice versa. Closes gh-34054
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizerFactory} implementation that provides support for
|
||||
@@ -51,10 +52,13 @@ class BeanOverrideContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
}
|
||||
|
||||
private void findBeanOverrideHandler(Class<?> testClass, Set<BeanOverrideHandler> handlers) {
|
||||
handlers.addAll(BeanOverrideHandler.forTestClass(testClass));
|
||||
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
|
||||
findBeanOverrideHandler(testClass.getEnclosingClass(), handlers);
|
||||
}
|
||||
BeanOverrideHandler.forTestClass(testClass).forEach(handler ->
|
||||
Assert.state(handlers.add(handler), () ->
|
||||
"Duplicate BeanOverrideHandler discovered in test class %s: %s"
|
||||
.formatted(testClass.getName(), handler)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user