Skip to content

Commit 7a6e401

Browse files
committed
Document visibility requirements for Bean Overrides
This commit makes it clear that there are no visibility requirements for @⁠TestBean fields or factory methods as well as @⁠MockitoBean or @⁠MockitoSpyBean fields. Closes gh-33923
1 parent 3569cfe commit 7a6e401

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ When using `@MockitoSpyBean` to create a spy for a `FactoryBean`, a spy will be
5252
for the object created by the `FactoryBean`, not for the `FactoryBean` itself.
5353
====
5454

55+
[NOTE]
56+
====
57+
There are no restrictions on the visibility of `@MockitoBean` and `@MockitoSpyBean`
58+
fields.
59+
60+
Such fields can therefore be `public`, `protected`, package-private (default visibility),
61+
or `private` depending on the needs or coding practices of the project.
62+
====
63+
5564
The following example shows how to use the default behavior of the `@MockitoBean` annotation:
5665

5766
[tabs]
@@ -62,7 +71,7 @@ Java::
6271
----
6372
class OverrideBeanTests {
6473
@MockitoBean // <1>
65-
private CustomService customService;
74+
CustomService customService;
6675
6776
// test case body...
6877
}
@@ -86,7 +95,7 @@ Java::
8695
----
8796
class OverrideBeanTests {
8897
@MockitoBean("service") // <1>
89-
private CustomService customService;
98+
CustomService customService;
9099
91100
// test case body...
92101
@@ -107,7 +116,7 @@ Java::
107116
----
108117
class OverrideBeanTests {
109118
@MockitoSpyBean // <1>
110-
private CustomService customService;
119+
CustomService customService;
111120
112121
// test case body...
113122
}
@@ -130,7 +139,7 @@ Java::
130139
----
131140
class OverrideBeanTests {
132141
@MockitoSpyBean("service") // <1>
133-
private CustomService customService;
142+
CustomService customService;
134143
135144
// test case body...
136145

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ same bean in several tests, make sure to name the field consistently to avoid cr
3030
unnecessary contexts.
3131
====
3232

33+
[NOTE]
34+
====
35+
There are no restrictions on the visibility of `@TestBean` fields or factory methods.
36+
37+
Such fields and methods can therefore be `public`, `protected`, package-private (default
38+
visibility), or `private` depending on the needs or coding practices of the project.
39+
====
40+
3341
The following example shows how to use the default behavior of the `@TestBean` annotation:
3442

3543
[tabs]
@@ -40,11 +48,11 @@ Java::
4048
----
4149
class OverrideBeanTests {
4250
@TestBean // <1>
43-
private CustomService customService;
51+
CustomService customService;
4452
4553
// test case body...
4654
47-
private static CustomService customService() { // <2>
55+
static CustomService customService() { // <2>
4856
return new MyFakeCustomService();
4957
}
5058
}
@@ -68,11 +76,11 @@ Java::
6876
----
6977
class OverrideBeanTests {
7078
@TestBean(name = "service", methodName = "createCustomService") // <1>
71-
private CustomService customService;
79+
CustomService customService;
7280
7381
// test case body...
7482
75-
private static CustomService createCustomService() { // <2>
83+
static CustomService createCustomService() { // <2>
7684
return new MyFakeCustomService();
7785
}
7886
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
* FactoryBean}, the {@code FactoryBean} will be replaced with a singleton bean
106106
* corresponding to the value returned from the {@code @TestBean} factory method.
107107
*
108+
* <p>There are no restrictions on the visibility of {@code @TestBean} fields or
109+
* factory methods. Such fields and methods can therefore be {@code public},
110+
* {@code protected}, package-private (default visibility), or {@code private}
111+
* depending on the needs or coding practices of the project.
112+
*
108113
* @author Simon Baslé
109114
* @author Stephane Nicoll
110115
* @author Sam Brannen

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

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
* FactoryBean}, the {@code FactoryBean} will be replaced with a singleton mock
5959
* of the type of object created by the {@code FactoryBean}.
6060
*
61+
* <p>There are no restrictions on the visibility of a {@code @MockitoBean} field.
62+
* Such fields can therefore be {@code public}, {@code protected}, package-private
63+
* (default visibility), or {@code private} depending on the needs or coding
64+
* practices of the project.
65+
*
6166
* @author Simon Baslé
6267
* @author Sam Brannen
6368
* @since 6.2

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

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
* a spy will be created for the object created by the {@code FactoryBean}, not
5252
* for the {@code FactoryBean} itself.
5353
*
54+
* <p>There are no restrictions on the visibility of a {@code @MockitoSpyBean} field.
55+
* Such fields can therefore be {@code public}, {@code protected}, package-private
56+
* (default visibility), or {@code private} depending on the needs or coding
57+
* practices of the project.
58+
*
5459
* @author Simon Baslé
5560
* @author Sam Brannen
5661
* @since 6.2

0 commit comments

Comments
 (0)