diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc index 6b6de7655d..acea0e8dcf 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc @@ -3,18 +3,17 @@ `@MockitoBean` and `@MockitoSpyBean` are used on fields in test classes to override beans in the test's `ApplicationContext` with a Mockito _mock_ or _spy_, respectively. In the -latter case, the original bean definition is not replaced, but instead an early instance -of the bean is captured and wrapped by the spy. +latter case, an early instance of the original bean is captured and wrapped by the spy. -By default, the annotated field's type is used to search for candidate bean definitions -to override. If multiple candidates match, `@Qualifier` can be provided to narrow the -candidate to override. Alternatively, a candidate whose bean definition name matches the -name of the field will match. +By default, the annotated field's type is used to search for candidate beans to override. +If multiple candidates match, `@Qualifier` can be provided to narrow the candidate to +override. Alternatively, a candidate whose bean name matches the name of the field will +match. -When using `@MockitoBean`, a new bean definition will be created if a corresponding bean -definition does not exist. However, if you would like for the test to fail when a -corresponding bean definition does not exist, you can set the `enforceOverride` attribute -to `true` – for example, `@MockitoBean(enforceOverride = true)`. +When using `@MockitoBean`, a new bean will be created if a corresponding bean does not +exist. However, if you would like for the test to fail when a corresponding bean does not +exist, you can set the `enforceOverride` attribute to `true` – for example, +`@MockitoBean(enforceOverride = true)`. To use a by-name override rather than a by-type override, specify the `name` attribute of the annotation. @@ -32,14 +31,16 @@ During the test class lifecycle, Mockito is set up via the `Mockito#mockitoSessi mechanism. Notably, it enables `STRICT_STUBS` mode by default. This can be changed on individual test classes with the `@MockitoBeanSettings` annotation. -The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION` +By default, the `@MockitoBean` annotation uses the `REPLACE_OR_CREATE` xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding]. -If no existing bean definition matches, a new bean definition is created on the fly. +If no existing bean matches, a new bean is created on the fly. As mentioned previously, +you can switch to the `REPLACE` strategy by setting the `enforceOverride` attribute to +`true`. -The `@MockitoSpyBean` annotation uses the `WRAP_BEAN` +The `@MockitoSpyBean` annotation uses the `WRAP` xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy], and the original instance is wrapped in a Mockito spy. This strategy requires that -exactly one candidate bean definition exists. +exactly one candidate bean exists. NOTE: Only _singleton_ beans can be overridden. Any attempt to override a non-singleton bean will result in an exception. diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc index 9ef336315d..cf76494332 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc @@ -10,15 +10,14 @@ have a return type compatible with the type of the bean to override. To make thi explicit, or if you'd rather use a different name, the annotation allows for a specific method name to be provided. -By default, the annotated field's type is used to search for candidate bean definitions -to override. If multiple candidates match, `@Qualifier` can be provided to narrow the -candidate to override. Alternatively, a candidate whose bean definition name matches the -name of the field will match. +By default, the annotated field's type is used to search for candidate beans to override. +If multiple candidates match, `@Qualifier` can be provided to narrow the candidate to +override. Alternatively, a candidate whose bean name matches the name of the field will +match. -A new bean definition will be created if a corresponding bean definition does not exist. -However, if you would like for the test to fail when a corresponding bean definition does -not exist, you can set the `enforceOverride` attribute to `true` – for example, -`@TestBean(enforceOverride = true)`. +A bean will be created if a corresponding bean does not exist. However, if you would like +for the test to fail when a corresponding bean does not exist, you can set the +`enforceOverride` attribute to `true` – for example, `@TestBean(enforceOverride = true)`. To use a by-name override rather than a by-type override, specify the `name` attribute of the annotation. diff --git a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/bean-overriding.adoc b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/bean-overriding.adoc index 23502063e9..02d78d23f7 100644 --- a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/bean-overriding.adoc +++ b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/bean-overriding.adoc @@ -46,14 +46,16 @@ with `@BeanOverride` and instantiates the corresponding `BeanOverrideProcessor` responsible for registering appropriate `OverrideMetadata`. The internal `BeanOverrideBeanFactoryPostProcessor` then uses that information to alter -the test's `ApplicationContext` by registering and replacing bean definitions as defined -by the corresponding `BeanOverrideStrategy`: +the test's `ApplicationContext` by registering and replacing beans as defined by the +corresponding `BeanOverrideStrategy`: -* `REPLACE_DEFINITION`: Replaces the bean definition. Throws an exception if a - corresponding bean definition does not exist. -* `REPLACE_OR_CREATE_DEFINITION`: Replaces the bean definition if it exists. Creates a - new bean definition if a corresponding bean definition does not exist. -* `WRAP_BEAN`: Retrieves the original bean instance and wraps it. +`REPLACE`:: + Replaces the bean. Throws an exception if a corresponding bean does not exist. +`REPLACE_OR_CREATE`:: + Replaces the bean if it exists. Creates a new bean if a corresponding bean does not + exist. +`WRAP`:: + Retrieves the original bean and wraps it. [NOTE] ==== diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java index c024d29360..89729a7d3c 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java @@ -106,14 +106,14 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, beanName, field.getDeclaringClass().getSimpleName(), field.getName())); switch (overrideMetadata.getStrategy()) { - case REPLACE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, true); - case REPLACE_OR_CREATE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, false); - case WRAP_BEAN -> wrapBean(beanFactory, overrideMetadata); + case REPLACE -> replaceBean(beanFactory, overrideMetadata, true); + case REPLACE_OR_CREATE -> replaceBean(beanFactory, overrideMetadata, false); + case WRAP -> wrapBean(beanFactory, overrideMetadata); } } - private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata, - boolean requireExistingDefinition) { + private void replaceBean(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata, + boolean requireExistingBean) { // NOTE: This method supports 3 distinct scenarios which must be accounted for. // @@ -124,7 +124,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, String beanName = overrideMetadata.getBeanName(); BeanDefinition existingBeanDefinition = null; if (beanName == null) { - beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingDefinition); + beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingBean); if (beanName != null) { // We are overriding an existing bean by-type. beanName = BeanFactoryUtils.transformedBeanName(beanName); @@ -146,9 +146,9 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, // We are overriding an existing bean by-name. existingBeanDefinition = beanFactory.getBeanDefinition(beanName); } - else if (requireExistingDefinition) { + else if (requireExistingBean) { throw new IllegalStateException(""" - Unable to override bean: there is no bean definition to replace \ + Unable to override bean: there is no bean to replace \ with name [%s] and type [%s].""" .formatted(beanName, overrideMetadata.getBeanType())); } @@ -237,7 +237,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Set candidates = getExistingBeanNamesByType(beanFactory, overrideMetadata, false); if (!candidates.contains(beanName)) { throw new IllegalStateException(""" - Unable to override bean by wrapping: there is no existing bean definition \ + Unable to override bean by wrapping: there is no existing bean \ with name [%s] and type [%s].""" .formatted(beanName, overrideMetadata.getBeanType())); } @@ -249,7 +249,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, @Nullable private String getBeanNameForType(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata, - boolean requireExistingDefinition) { + boolean requireExistingBean) { Set candidateNames = getExistingBeanNamesByType(beanFactory, overrideMetadata, true); int candidateCount = candidateNames.size(); @@ -257,10 +257,10 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, return candidateNames.iterator().next(); } else if (candidateCount == 0) { - if (requireExistingDefinition) { + if (requireExistingBean) { Field field = overrideMetadata.getField(); throw new IllegalStateException( - "Unable to override bean: no bean definitions of type %s (as required by annotated field '%s.%s')" + "Unable to override bean: no beans of type %s (as required by annotated field '%s.%s')" .formatted(overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(), field.getName())); } return null; @@ -268,7 +268,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Field field = overrideMetadata.getField(); throw new IllegalStateException(""" - Unable to select a bean definition to override: found %s bean definitions of type %s \ + Unable to select a bean to override: found %s beans of type %s \ (as required by annotated field '%s.%s'): %s""" .formatted(candidateCount, overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(), field.getName(), candidateNames)); diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideRegistrar.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideRegistrar.java index 098bff4bb6..2269dbc04b 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideRegistrar.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideRegistrar.java @@ -57,7 +57,7 @@ class BeanOverrideRegistrar { */ Object wrapIfNecessary(Object bean, String beanName) throws BeansException { OverrideMetadata metadata = this.earlyOverrideMetadata.get(beanName); - if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP_BEAN) { + if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP) { bean = metadata.createOverride(beanName, null, bean); metadata.track(bean, this.beanFactory); } diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideStrategy.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideStrategy.java index 72e459cb01..b0f9437fb5 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideStrategy.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideStrategy.java @@ -27,27 +27,24 @@ package org.springframework.test.context.bean.override; public enum BeanOverrideStrategy { /** - * Replace a given bean definition, immediately preparing a singleton instance. - *

Fails if the original bean definition does not exist. To create a new bean - * definition in such a case, use {@link #REPLACE_OR_CREATE_DEFINITION} instead. + * Replace a given bean, immediately preparing a singleton instance. + *

Fails if the original bean does not exist. To create a new bean + * in such a case, use {@link #REPLACE_OR_CREATE} instead. */ - REPLACE_DEFINITION, + REPLACE, /** - * Replace or create a given bean definition, immediately preparing a - * singleton instance. - *

Contrary to {@link #REPLACE_DEFINITION}, this creates a new bean - * definition if the target bean definition does not exist rather than - * failing. + * Replace or create a given bean, immediately preparing a singleton instance. + *

Contrary to {@link #REPLACE}, this strategy creates a new bean if the + * target bean does not exist rather than failing. */ - REPLACE_OR_CREATE_DEFINITION, + REPLACE_OR_CREATE, /** - * Intercept and process an early bean reference rather than a bean - * definition, allowing variants of bean overriding to wrap the instance - * — for example, to delegate to actual methods in the context of a - * mocking "spy". + * Intercept and process an early bean reference, allowing variants of bean + * overriding to wrap the original bean instance — for example, to + * delegate to actual methods in the context of a mocking "spy". */ - WRAP_BEAN + WRAP } diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/WrapEarlyBeanPostProcessor.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/WrapEarlyBeanPostProcessor.java index 4c5ebf5fbf..bee9588dec 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/WrapEarlyBeanPostProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/WrapEarlyBeanPostProcessor.java @@ -28,8 +28,8 @@ import org.springframework.util.StringUtils; /** * {@link SmartInstantiationAwareBeanPostProcessor} implementation that wraps - * beans in order to support the {@link BeanOverrideStrategy#WRAP_BEAN WRAP_BEAN} - * bean override strategy. + * beans in order to support the {@link BeanOverrideStrategy#WRAP WRAP} bean + * override strategy. * * @author Simon Baslé * @author Stephane Nicoll diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java index 0bd3d72f3e..064075ab1f 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java @@ -154,8 +154,8 @@ public @interface TestBean { * be created if a corresponding bean definition does not exist. *

Set to {@code true} to cause an exception to be thrown if a corresponding * bean definition does not exist. - * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION - * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION + * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE + * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE */ boolean enforceOverride() default false; diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java index cd17f52d13..b533aea558 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java @@ -37,8 +37,8 @@ import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.MethodFilter; -import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_DEFINITION; -import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION; +import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE; +import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE; /** * {@link BeanOverrideProcessor} implementation for {@link TestBean @TestBean} @@ -62,7 +62,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor { String beanName = (!testBean.name().isBlank() ? testBean.name() : null); String methodName = testBean.methodName(); - BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE_DEFINITION : REPLACE_OR_CREATE_DEFINITION); + BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE); Method overrideMethod; if (!methodName.isBlank()) { diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBean.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBean.java index aa221ac4ac..e3cf968b27 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBean.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBean.java @@ -124,8 +124,8 @@ public @interface MockitoBean { * be created if a corresponding bean definition does not exist. *

Set to {@code true} to cause an exception to be thrown if a corresponding * bean definition does not exist. - * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION - * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION + * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE + * @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE */ boolean enforceOverride() default false; diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideMetadata.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideMetadata.java index 8de7f9e51f..0037ceee24 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideMetadata.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideMetadata.java @@ -37,8 +37,8 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; -import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_DEFINITION; -import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION; +import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE; +import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE; /** * {@link OverrideMetadata} implementation for Mockito {@code mock} support. @@ -59,7 +59,7 @@ class MockitoBeanOverrideMetadata extends AbstractMockitoOverrideMetadata { MockitoBeanOverrideMetadata(Field field, ResolvableType typeToMock, MockitoBean mockitoBean) { this(field, typeToMock, (!mockitoBean.name().isBlank() ? mockitoBean.name() : null), - (mockitoBean.enforceOverride() ? REPLACE_DEFINITION : REPLACE_OR_CREATE_DEFINITION), + (mockitoBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE), mockitoBean.reset(), mockitoBean.extraInterfaces(), mockitoBean.answers(), mockitoBean.serializable()); } diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java index ed6234c8f8..f3c5a2714d 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideMetadata.java @@ -52,7 +52,7 @@ class MockitoSpyBeanOverrideMetadata extends AbstractMockitoOverrideMetadata { MockitoSpyBeanOverrideMetadata(Field field, ResolvableType typeToSpy, @Nullable String beanName, MockReset reset, boolean proxyTargetAware) { - super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP_BEAN, reset, proxyTargetAware); + super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP, reset, proxyTargetAware); Assert.notNull(typeToSpy, "typeToSpy must not be null"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessorTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessorTests.java index d2c2fbcd71..7e9fc86b0e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessorTests.java @@ -83,7 +83,7 @@ class BeanOverrideBeanFactoryPostProcessorTests { assertThatIllegalStateException() .isThrownBy(context::refresh) - .withMessage("Unable to override bean: there is no bean definition " + + .withMessage("Unable to override bean: there is no bean " + "to replace with name [descriptionBean] and type [java.lang.String]."); } @@ -94,7 +94,7 @@ class BeanOverrideBeanFactoryPostProcessorTests { assertThatIllegalStateException() .isThrownBy(context::refresh) - .withMessage("Unable to override bean: there is no bean definition " + + .withMessage("Unable to override bean: there is no bean " + "to replace with name [descriptionBean] and type [java.lang.String]."); } @@ -140,7 +140,7 @@ class BeanOverrideBeanFactoryPostProcessorTests { assertThatIllegalStateException() .isThrownBy(context::refresh) - .withMessage("Unable to override bean: no bean definitions of type java.lang.Integer " + + .withMessage("Unable to override bean: no beans of type java.lang.Integer " + "(as required by annotated field 'CaseByType.counter')"); } @@ -152,7 +152,7 @@ class BeanOverrideBeanFactoryPostProcessorTests { assertThatIllegalStateException() .isThrownBy(context::refresh) - .withMessage("Unable to select a bean definition to override: found 2 bean definitions " + + .withMessage("Unable to select a bean to override: found 2 beans " + "of type java.lang.Integer (as required by annotated field 'CaseByType.counter'): " + "[someInteger, anotherInteger]"); } @@ -388,24 +388,24 @@ class BeanOverrideBeanFactoryPostProcessorTests { static class CaseByNameWithReplaceOrCreateStrategy { - @DummyBean(beanName = "descriptionBean", strategy = BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION) + @DummyBean(beanName = "descriptionBean", strategy = BeanOverrideStrategy.REPLACE_OR_CREATE) private String description; } static class CaseByTypeWithReplaceOrCreateStrategy { - @DummyBean(strategy = BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION) + @DummyBean(strategy = BeanOverrideStrategy.REPLACE_OR_CREATE) private String description; } static class CaseByNameAndByTypeWithReplaceOrCreateStrategy { - @DummyBean(beanName = "descriptionBean", strategy = BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION) + @DummyBean(beanName = "descriptionBean", strategy = BeanOverrideStrategy.REPLACE_OR_CREATE) private String description; - @DummyBean(strategy = BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION) + @DummyBean(strategy = BeanOverrideStrategy.REPLACE_OR_CREATE) private Integer counter; } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerFactoryTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerFactoryTests.java index bf70345024..61474dcb1f 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerFactoryTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerFactoryTests.java @@ -69,7 +69,7 @@ class BeanOverrideContextCustomizerFactoryTests { private Consumer dummyMetadata(@Nullable String beanName, Class beanType) { - return dummyMetadata(beanName, beanType, BeanOverrideStrategy.REPLACE_DEFINITION); + return dummyMetadata(beanName, beanType, BeanOverrideStrategy.REPLACE); } private Consumer dummyMetadata(@Nullable String beanName, Class beanType, BeanOverrideStrategy strategy) { diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerTests.java index a4a06d3021..f8fec2f07a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerTests.java @@ -72,7 +72,7 @@ class BeanOverrideContextCustomizerTests { public DummyOverrideMetadata(String key) { super(ReflectionUtils.findField(DummyOverrideMetadata.class, "key"), - ResolvableType.forClass(Object.class), null, BeanOverrideStrategy.REPLACE_DEFINITION); + ResolvableType.forClass(Object.class), null, BeanOverrideStrategy.REPLACE); this.key = key; } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/DummyBean.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/DummyBean.java index 55f01fd035..9004731ba5 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/DummyBean.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/DummyBean.java @@ -45,7 +45,7 @@ import org.springframework.util.StringUtils; String beanName() default ""; - BeanOverrideStrategy strategy() default BeanOverrideStrategy.REPLACE_DEFINITION; + BeanOverrideStrategy strategy() default BeanOverrideStrategy.REPLACE; class DummyBeanOverrideProcessor implements BeanOverrideProcessor { diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/OverrideMetadataTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/OverrideMetadataTests.java index 79071b5e45..8883ca5929 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/OverrideMetadataTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/OverrideMetadataTests.java @@ -185,7 +185,7 @@ class OverrideMetadataTests { } private static OverrideMetadata createMetadata(Field field, @Nullable String name) { - return new DummyOverrideMetadata(field, field.getType(), name, BeanOverrideStrategy.REPLACE_DEFINITION); + return new DummyOverrideMetadata(field, field.getType(), name, BeanOverrideStrategy.REPLACE); } private static Field field(Class target, String fieldName) { @@ -195,7 +195,7 @@ class OverrideMetadataTests { } private static Consumer hasOverrideMetadata(Field field, Class beanType, @Nullable String beanName) { - return hasOverrideMetadata(field, beanType, BeanOverrideStrategy.REPLACE_DEFINITION, beanName); + return hasOverrideMetadata(field, beanType, BeanOverrideStrategy.REPLACE, beanName); } private static Consumer hasOverrideMetadata(Field field, Class beanType, BeanOverrideStrategy strategy, diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideMetadataTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideMetadataTests.java index fb5bbb0f91..08ce97aa8a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideMetadataTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideMetadataTests.java @@ -126,7 +126,7 @@ class TestBeanOverrideMetadataTests { TestBean annotation = field.getAnnotation(TestBean.class); String beanName = (StringUtils.hasText(annotation.name()) ? annotation.name() : null); return new TestBeanOverrideMetadata( - field, ResolvableType.forClass(field.getType()), beanName, BeanOverrideStrategy.REPLACE_DEFINITION, overrideMethod); + field, ResolvableType.forClass(field.getType()), beanName, BeanOverrideStrategy.REPLACE, overrideMethod); } static class SampleOneOverride { diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanTests.java index 5fa8d58845..8a1063e535 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanTests.java @@ -40,7 +40,7 @@ public class TestBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: there is no bean definition \ + Unable to override bean: there is no bean \ to replace with name [beanToOverride] and type [java.lang.String]."""); } @@ -52,7 +52,7 @@ public class TestBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: there is no bean definition \ + Unable to override bean: there is no bean \ to replace with name [beanToOverride] and type [java.lang.String]."""); } @@ -63,7 +63,7 @@ public class TestBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: no bean definitions of \ + Unable to override bean: no beans of \ type %s (as required by annotated field '%s.example')""".formatted( String.class.getName(), FailureByTypeLookup.class.getSimpleName())); } @@ -77,7 +77,7 @@ public class TestBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to select a bean definition to override: found 2 bean definitions \ + Unable to select a bean to override: found 2 beans \ of type %s (as required by annotated field '%s.example'): %s""".formatted( String.class.getName(), FailureByTypeLookup.class.getSimpleName(), List.of("bean1", "bean2"))); } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/easymock/EasyMockBeanOverrideMetadata.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/easymock/EasyMockBeanOverrideMetadata.java index 96b9e3c13d..4ce02a702e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/easymock/EasyMockBeanOverrideMetadata.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/easymock/EasyMockBeanOverrideMetadata.java @@ -27,7 +27,7 @@ import org.springframework.core.ResolvableType; import org.springframework.lang.Nullable; import org.springframework.test.context.bean.override.OverrideMetadata; -import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION; +import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE; /** * {@link OverrideMetadata} that provides support for {@link EasyMockBean @EasyMockBean}. @@ -43,7 +43,7 @@ class EasyMockBeanOverrideMetadata extends OverrideMetadata { EasyMockBeanOverrideMetadata(Field field, Class typeToOverride, @Nullable String beanName, MockType mockType) { - super(field, ResolvableType.forClass(typeToOverride), beanName, REPLACE_OR_CREATE_DEFINITION); + super(field, ResolvableType.forClass(typeToOverride), beanName, REPLACE_OR_CREATE); this.mockType = mockType; } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoMockBeanTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoMockBeanTests.java index 524bc9fe0a..4addd24ebf 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoMockBeanTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoMockBeanTests.java @@ -41,7 +41,7 @@ class MockitoMockBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: there is no bean definition \ + Unable to override bean: there is no bean \ to replace with name [beanToOverride] and type [java.lang.String]."""); } @@ -53,7 +53,7 @@ class MockitoMockBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: there is no bean definition \ + Unable to override bean: there is no bean \ to replace with name [beanToOverride] and type [java.lang.String]."""); } @@ -64,7 +64,7 @@ class MockitoMockBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to override bean: no bean definitions of \ + Unable to override bean: no beans of \ type %s (as required by annotated field '%s.example')""".formatted( String.class.getName(), FailureByTypeLookup.class.getSimpleName())); } @@ -78,7 +78,7 @@ class MockitoMockBeanTests { assertThatIllegalStateException() .isThrownBy(context::refresh) .withMessage(""" - Unable to select a bean definition to override: found 2 bean definitions \ + Unable to select a bean to override: found 2 beans \ of type %s (as required by annotated field '%s.example'): %s""".formatted( String.class.getName(), FailureByTypeLookup.class.getSimpleName(), List.of("bean1", "bean2"))); } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanTests.java index fce7ab3f73..b788ba6561 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanTests.java @@ -41,7 +41,7 @@ class MockitoSpyBeanTests { .isThrownBy(context::refresh) .withMessage(""" Unable to override bean by wrapping: \ - there is no existing bean definition with name [beanToSpy] and type [%s].""", + there is no existing bean with name [beanToSpy] and type [%s].""", String.class.getName()); }