Skip to content

Commit 67cb3c7

Browse files
committed
Rename BeanOverrideStrategy enum constants
Closes gh-33701
1 parent 7ea43bb commit 67cb3c7

22 files changed

+97
-98
lines changed

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33

44
`@MockitoBean` and `@MockitoSpyBean` are used on fields in test classes to override beans
55
in the test's `ApplicationContext` with a Mockito _mock_ or _spy_, respectively. In the
6-
latter case, the original bean definition is not replaced, but instead an early instance
7-
of the bean is captured and wrapped by the spy.
6+
latter case, an early instance of the original bean is captured and wrapped by the spy.
87

9-
By default, the annotated field's type is used to search for candidate bean definitions
10-
to override. If multiple candidates match, `@Qualifier` can be provided to narrow the
11-
candidate to override. Alternatively, a candidate whose bean definition name matches the
12-
name of the field will match.
8+
By default, the annotated field's type is used to search for candidate beans to override.
9+
If multiple candidates match, `@Qualifier` can be provided to narrow the candidate to
10+
override. Alternatively, a candidate whose bean name matches the name of the field will
11+
match.
1312

14-
When using `@MockitoBean`, a new bean definition will be created if a corresponding bean
15-
definition does not exist. However, if you would like for the test to fail when a
16-
corresponding bean definition does not exist, you can set the `enforceOverride` attribute
17-
to `true` – for example, `@MockitoBean(enforceOverride = true)`.
13+
When using `@MockitoBean`, a new bean will be created if a corresponding bean does not
14+
exist. However, if you would like for the test to fail when a corresponding bean does not
15+
exist, you can set the `enforceOverride` attribute to `true` – for example,
16+
`@MockitoBean(enforceOverride = true)`.
1817

1918
To use a by-name override rather than a by-type override, specify the `name` attribute
2019
of the annotation.
@@ -32,14 +31,16 @@ During the test class lifecycle, Mockito is set up via the `Mockito#mockitoSessi
3231
mechanism. Notably, it enables `STRICT_STUBS` mode by default. This can be changed on
3332
individual test classes with the `@MockitoBeanSettings` annotation.
3433

35-
The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION`
34+
By default, the `@MockitoBean` annotation uses the `REPLACE_OR_CREATE`
3635
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
37-
If no existing bean definition matches, a new bean definition is created on the fly.
36+
If no existing bean matches, a new bean is created on the fly. As mentioned previously,
37+
you can switch to the `REPLACE` strategy by setting the `enforceOverride` attribute to
38+
`true`.
3839

39-
The `@MockitoSpyBean` annotation uses the `WRAP_BEAN`
40+
The `@MockitoSpyBean` annotation uses the `WRAP`
4041
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy],
4142
and the original instance is wrapped in a Mockito spy. This strategy requires that
42-
exactly one candidate bean definition exists.
43+
exactly one candidate bean exists.
4344

4445
NOTE: Only _singleton_ beans can be overridden. Any attempt to override a non-singleton
4546
bean will result in an exception.

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ have a return type compatible with the type of the bean to override. To make thi
1010
explicit, or if you'd rather use a different name, the annotation allows for a specific
1111
method name to be provided.
1212

13-
By default, the annotated field's type is used to search for candidate bean definitions
14-
to override. If multiple candidates match, `@Qualifier` can be provided to narrow the
15-
candidate to override. Alternatively, a candidate whose bean definition name matches the
16-
name of the field will match.
17-
18-
A new bean definition will be created if a corresponding bean definition does not exist.
19-
However, if you would like for the test to fail when a corresponding bean definition does
20-
not exist, you can set the `enforceOverride` attribute to `true` – for example,
21-
`@TestBean(enforceOverride = true)`.
13+
By default, the annotated field's type is used to search for candidate beans to override.
14+
If multiple candidates match, `@Qualifier` can be provided to narrow the candidate to
15+
override. Alternatively, a candidate whose bean name matches the name of the field will
16+
match.
17+
18+
A bean will be created if a corresponding bean does not exist. However, if you would like
19+
for the test to fail when a corresponding bean does not exist, you can set the
20+
`enforceOverride` attribute to `true` – for example, `@TestBean(enforceOverride = true)`.
2221

2322
To use a by-name override rather than a by-type override, specify the `name` attribute
2423
of the annotation.

framework-docs/modules/ROOT/pages/testing/testcontext-framework/bean-overriding.adoc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ with `@BeanOverride` and instantiates the corresponding `BeanOverrideProcessor`
4646
responsible for registering appropriate `OverrideMetadata`.
4747

4848
The internal `BeanOverrideBeanFactoryPostProcessor` then uses that information to alter
49-
the test's `ApplicationContext` by registering and replacing bean definitions as defined
50-
by the corresponding `BeanOverrideStrategy`:
51-
52-
* `REPLACE_DEFINITION`: Replaces the bean definition. Throws an exception if a
53-
corresponding bean definition does not exist.
54-
* `REPLACE_OR_CREATE_DEFINITION`: Replaces the bean definition if it exists. Creates a
55-
new bean definition if a corresponding bean definition does not exist.
56-
* `WRAP_BEAN`: Retrieves the original bean instance and wraps it.
49+
the test's `ApplicationContext` by registering and replacing beans as defined by the
50+
corresponding `BeanOverrideStrategy`:
51+
52+
`REPLACE`::
53+
Replaces the bean. Throws an exception if a corresponding bean does not exist.
54+
`REPLACE_OR_CREATE`::
55+
Replaces the bean if it exists. Creates a new bean if a corresponding bean does not
56+
exist.
57+
`WRAP`::
58+
Retrieves the original bean and wraps it.
5759

5860
[NOTE]
5961
====

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ private void registerBeanOverride(ConfigurableListableBeanFactory beanFactory, O
106106
beanName, field.getDeclaringClass().getSimpleName(), field.getName()));
107107

108108
switch (overrideMetadata.getStrategy()) {
109-
case REPLACE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, true);
110-
case REPLACE_OR_CREATE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, false);
111-
case WRAP_BEAN -> wrapBean(beanFactory, overrideMetadata);
109+
case REPLACE -> replaceBean(beanFactory, overrideMetadata, true);
110+
case REPLACE_OR_CREATE -> replaceBean(beanFactory, overrideMetadata, false);
111+
case WRAP -> wrapBean(beanFactory, overrideMetadata);
112112
}
113113
}
114114

115-
private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
116-
boolean requireExistingDefinition) {
115+
private void replaceBean(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
116+
boolean requireExistingBean) {
117117

118118
// NOTE: This method supports 3 distinct scenarios which must be accounted for.
119119
//
@@ -124,7 +124,7 @@ private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, Over
124124
String beanName = overrideMetadata.getBeanName();
125125
BeanDefinition existingBeanDefinition = null;
126126
if (beanName == null) {
127-
beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingDefinition);
127+
beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingBean);
128128
if (beanName != null) {
129129
// We are overriding an existing bean by-type.
130130
beanName = BeanFactoryUtils.transformedBeanName(beanName);
@@ -146,9 +146,9 @@ private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, Over
146146
// We are overriding an existing bean by-name.
147147
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
148148
}
149-
else if (requireExistingDefinition) {
149+
else if (requireExistingBean) {
150150
throw new IllegalStateException("""
151-
Unable to override bean: there is no bean definition to replace \
151+
Unable to override bean: there is no bean to replace \
152152
with name [%s] and type [%s]."""
153153
.formatted(beanName, overrideMetadata.getBeanType()));
154154
}
@@ -237,7 +237,7 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, OverrideMetad
237237
Set<String> candidates = getExistingBeanNamesByType(beanFactory, overrideMetadata, false);
238238
if (!candidates.contains(beanName)) {
239239
throw new IllegalStateException("""
240-
Unable to override bean by wrapping: there is no existing bean definition \
240+
Unable to override bean by wrapping: there is no existing bean \
241241
with name [%s] and type [%s]."""
242242
.formatted(beanName, overrideMetadata.getBeanType()));
243243
}
@@ -249,26 +249,26 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, OverrideMetad
249249

250250
@Nullable
251251
private String getBeanNameForType(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
252-
boolean requireExistingDefinition) {
252+
boolean requireExistingBean) {
253253

254254
Set<String> candidateNames = getExistingBeanNamesByType(beanFactory, overrideMetadata, true);
255255
int candidateCount = candidateNames.size();
256256
if (candidateCount == 1) {
257257
return candidateNames.iterator().next();
258258
}
259259
else if (candidateCount == 0) {
260-
if (requireExistingDefinition) {
260+
if (requireExistingBean) {
261261
Field field = overrideMetadata.getField();
262262
throw new IllegalStateException(
263-
"Unable to override bean: no bean definitions of type %s (as required by annotated field '%s.%s')"
263+
"Unable to override bean: no beans of type %s (as required by annotated field '%s.%s')"
264264
.formatted(overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(), field.getName()));
265265
}
266266
return null;
267267
}
268268

269269
Field field = overrideMetadata.getField();
270270
throw new IllegalStateException("""
271-
Unable to select a bean definition to override: found %s bean definitions of type %s \
271+
Unable to select a bean to override: found %s beans of type %s \
272272
(as required by annotated field '%s.%s'): %s"""
273273
.formatted(candidateCount, overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(),
274274
field.getName(), candidateNames));

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class BeanOverrideRegistrar {
5757
*/
5858
Object wrapIfNecessary(Object bean, String beanName) throws BeansException {
5959
OverrideMetadata metadata = this.earlyOverrideMetadata.get(beanName);
60-
if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP_BEAN) {
60+
if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP) {
6161
bean = metadata.createOverride(beanName, null, bean);
6262
metadata.track(bean, this.beanFactory);
6363
}

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideStrategy.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,24 @@
2727
public enum BeanOverrideStrategy {
2828

2929
/**
30-
* Replace a given bean definition, immediately preparing a singleton instance.
31-
* <p>Fails if the original bean definition does not exist. To create a new bean
32-
* definition in such a case, use {@link #REPLACE_OR_CREATE_DEFINITION} instead.
30+
* Replace a given bean, immediately preparing a singleton instance.
31+
* <p>Fails if the original bean does not exist. To create a new bean
32+
* in such a case, use {@link #REPLACE_OR_CREATE} instead.
3333
*/
34-
REPLACE_DEFINITION,
34+
REPLACE,
3535

3636
/**
37-
* Replace or create a given bean definition, immediately preparing a
38-
* singleton instance.
39-
* <p>Contrary to {@link #REPLACE_DEFINITION}, this creates a new bean
40-
* definition if the target bean definition does not exist rather than
41-
* failing.
37+
* Replace or create a given bean, immediately preparing a singleton instance.
38+
* <p>Contrary to {@link #REPLACE}, this strategy creates a new bean if the
39+
* target bean does not exist rather than failing.
4240
*/
43-
REPLACE_OR_CREATE_DEFINITION,
41+
REPLACE_OR_CREATE,
4442

4543
/**
46-
* Intercept and process an early bean reference rather than a bean
47-
* definition, allowing variants of bean overriding to wrap the instance
48-
* &mdash; for example, to delegate to actual methods in the context of a
49-
* mocking "spy".
44+
* Intercept and process an early bean reference, allowing variants of bean
45+
* overriding to wrap the original bean instance &mdash; for example, to
46+
* delegate to actual methods in the context of a mocking "spy".
5047
*/
51-
WRAP_BEAN
48+
WRAP
5249

5350
}

spring-test/src/main/java/org/springframework/test/context/bean/override/WrapEarlyBeanPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
/**
3030
* {@link SmartInstantiationAwareBeanPostProcessor} implementation that wraps
31-
* beans in order to support the {@link BeanOverrideStrategy#WRAP_BEAN WRAP_BEAN}
32-
* bean override strategy.
31+
* beans in order to support the {@link BeanOverrideStrategy#WRAP WRAP} bean
32+
* override strategy.
3333
*
3434
* @author Simon Baslé
3535
* @author Stephane Nicoll

spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@
154154
* be created if a corresponding bean definition does not exist.
155155
* <p>Set to {@code true} to cause an exception to be thrown if a corresponding
156156
* bean definition does not exist.
157-
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION
158-
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION
157+
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE
158+
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE
159159
*/
160160
boolean enforceOverride() default false;
161161

spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
import org.springframework.util.ReflectionUtils;
3838
import org.springframework.util.ReflectionUtils.MethodFilter;
3939

40-
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_DEFINITION;
41-
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION;
40+
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE;
41+
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE;
4242

4343
/**
4444
* {@link BeanOverrideProcessor} implementation for {@link TestBean @TestBean}
@@ -62,7 +62,7 @@ public TestBeanOverrideMetadata createMetadata(Annotation overrideAnnotation, Cl
6262

6363
String beanName = (!testBean.name().isBlank() ? testBean.name() : null);
6464
String methodName = testBean.methodName();
65-
BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE_DEFINITION : REPLACE_OR_CREATE_DEFINITION);
65+
BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE);
6666

6767
Method overrideMethod;
6868
if (!methodName.isBlank()) {

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@
124124
* be created if a corresponding bean definition does not exist.
125125
* <p>Set to {@code true} to cause an exception to be thrown if a corresponding
126126
* bean definition does not exist.
127-
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION
128-
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION
127+
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE
128+
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE
129129
*/
130130
boolean enforceOverride() default false;
131131

0 commit comments

Comments
 (0)