@@ -11,29 +11,21 @@ If multiple candidates match, `@Qualifier` can be provided to narrow the candida
11
11
override. Alternatively, a candidate whose bean name matches the name of the field will
12
12
match.
13
13
14
- When using `@MockitoBean`, a new bean will be created if a corresponding bean does not
15
- exist. However, if you would like for the test to fail when a corresponding bean does not
16
- exist, you can set the `enforceOverride` attribute to `true` – for example,
17
- `@MockitoBean(enforceOverride = true)`.
18
-
19
- To use a by-name override rather than a by-type override, specify the `name` attribute
20
- of the annotation.
21
-
22
14
[WARNING]
23
15
====
24
16
Qualifiers, including the name of the field, are used to determine if a separate
25
17
`ApplicationContext` needs to be created. If you are using this feature to mock or spy
26
- the same bean in several tests , make sure to name the field consistently to avoid
18
+ the same bean in several test classes , make sure to name the field consistently to avoid
27
19
creating unnecessary contexts.
28
20
====
29
21
30
- Each annotation also defines Mockito-specific attributes to fine-tune the mocking details .
22
+ Each annotation also defines Mockito-specific attributes to fine-tune the mocking behavior .
31
23
32
- By default, the `@MockitoBean` annotation uses the `REPLACE_OR_CREATE`
24
+ The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE`
33
25
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
34
- If no existing bean matches, a new bean is created on the fly. As mentioned previously,
35
- you can switch to the `REPLACE` strategy by setting the `enforceOverride` attribute to
36
- `true` .
26
+ If no existing bean matches, a new bean is created on the fly. However, you can switch to
27
+ the `REPLACE` strategy by setting the `enforceOverride` attribute to `true`. See the
28
+ following section for an example .
37
29
38
30
The `@MockitoSpyBean` annotation uses the `WRAP`
39
31
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy],
@@ -61,6 +53,17 @@ Such fields can therefore be `public`, `protected`, package-private (default vis
61
53
or `private` depending on the needs or coding practices of the project.
62
54
====
63
55
56
+ [[spring-testing-annotation-beanoverriding-mockitobean-examples]]
57
+ == `@MockitoBean` Examples
58
+
59
+ When using `@MockitoBean`, a new bean will be created if a corresponding bean does not
60
+ exist. However, if you would like for the test to fail when a corresponding bean does not
61
+ exist, you can set the `enforceOverride` attribute to `true` – for example,
62
+ `@MockitoBean(enforceOverride = true)`.
63
+
64
+ To use a by-name override rather than a by-type override, specify the `name` (or `value`)
65
+ attribute of the annotation.
66
+
64
67
The following example shows how to use the default behavior of the `@MockitoBean` annotation:
65
68
66
69
[tabs]
@@ -69,11 +72,13 @@ Java::
69
72
+
70
73
[source,java,indent=0,subs="verbatim,quotes"]
71
74
----
72
- class OverrideBeanTests {
75
+ @SpringJUnitConfig(TestConfig.class)
76
+ class BeanOverrideTests {
77
+
73
78
@MockitoBean // <1>
74
79
CustomService customService;
75
80
76
- // test case body ...
81
+ // tests ...
77
82
}
78
83
----
79
84
<1> Replace the bean with type `CustomService` with a Mockito `mock`.
82
87
In the example above, we are creating a mock for `CustomService`. If more than one bean
83
88
of that type exists, the bean named `customService` is considered. Otherwise, the test
84
89
will fail, and you will need to provide a qualifier of some sort to identify which of the
85
- `CustomService` beans you want to override. If no such bean exists, a bean definition
86
- will be created with an auto-generated bean name.
90
+ `CustomService` beans you want to override. If no such bean exists, a bean will be
91
+ created with an auto-generated bean name.
87
92
88
93
The following example uses a by-name lookup, rather than a by-type lookup:
89
94
@@ -93,32 +98,114 @@ Java::
93
98
+
94
99
[source,java,indent=0,subs="verbatim,quotes"]
95
100
----
96
- class OverrideBeanTests {
101
+ @SpringJUnitConfig(TestConfig.class)
102
+ class BeanOverrideTests {
103
+
97
104
@MockitoBean("service") // <1>
98
105
CustomService customService;
99
106
100
- // test case body ...
107
+ // tests ...
101
108
102
109
}
103
110
----
104
111
<1> Replace the bean named `service` with a Mockito `mock`.
105
112
======
106
113
107
- If no bean definition named `service` exists, one is created.
114
+ If no bean named `service` exists, one is created.
115
+
116
+ `@MockitoBean` can also be used at the type level:
117
+
118
+ - on a test class or any superclass or implemented interface in the type hierarchy above
119
+ the test class
120
+ - on an enclosing class for a `@Nested` test class or on any class or interface in the
121
+ type hierarchy or enclosing class hierarchy above the `@Nested` test class
108
122
109
- The following example shows how to use the default behavior of the `@MockitoSpyBean` annotation:
123
+ When `@MockitoBean` is declared at the type level, the type of bean (or beans) to mock
124
+ must be supplied via the `types` attribute – for example,
125
+ `@MockitoBean(types = {OrderService.class, UserService.class})`. If multiple candidates
126
+ exist in the application context, you can explicitly specify a bean name to mock by
127
+ setting the `name` attribute. Note, however, that the `types` attribute must contain a
128
+ single type if an explicit bean `name` is configured – for example,
129
+ `@MockitoBean(name = "ps1", types = PrintingService.class)`.
130
+
131
+ To support reuse of mock configuration, `@MockitoBean` may be used as a meta-annotation
132
+ to create custom _composed annotations_ — for example, to define common mock
133
+ configuration in a single annotation that can be reused across a test suite.
134
+ `@MockitoBean` can also be used as a repeatable annotation at the type level — for
135
+ example, to mock several beans by name.
136
+
137
+ The following `@SharedMocks` annotation registers two mocks by-type and one mock by-name.
110
138
111
139
[tabs]
112
140
======
113
141
Java::
114
142
+
115
143
[source,java,indent=0,subs="verbatim,quotes"]
116
144
----
117
- class OverrideBeanTests {
145
+ @Target(ElementType.TYPE)
146
+ @Retention(RetentionPolicy.RUNTIME)
147
+ @MockitoBean(types = {OrderService.class, UserService.class}) // <1>
148
+ @MockitoBean(name = "ps1", types = PrintingService.class) // <2>
149
+ public @interface SharedMocks {
150
+ }
151
+ ----
152
+ <1> Register `OrderService` and `UserService` mocks by-type.
153
+ <2> Register `PrintingService` mock by-name.
154
+ ======
155
+
156
+ The following demonstrates how `@SharedMocks` can be used on a test class.
157
+
158
+ [tabs]
159
+ ======
160
+ Java::
161
+ +
162
+ [source,java,indent=0,subs="verbatim,quotes"]
163
+ ----
164
+ @SpringJUnitConfig(TestConfig.class)
165
+ @SharedMocks // <1>
166
+ class BeanOverrideTests {
167
+
168
+ @Autowired OrderService orderService; // <2>
169
+
170
+ @Autowired UserService userService; // <2>
171
+
172
+ @Autowired PrintingService ps1; // <2>
173
+
174
+ // Inject other components that rely on the mocks.
175
+
176
+ @Test
177
+ void testThatDependsOnMocks() {
178
+ // ...
179
+ }
180
+ }
181
+ ----
182
+ <1> Register common mocks via the custom `@SharedMocks` annotation.
183
+ <2> Optionally inject mocks to _stub_ or _verify_ them.
184
+ ======
185
+
186
+ TIP: The mocks can also be injected into `@Configuration` classes or other test-related
187
+ components in the `ApplicationContext` in order to configure them with Mockito's stubbing
188
+ APIs.
189
+
190
+ [[spring-testing-annotation-beanoverriding-mockitospybean-examples]]
191
+ == `@MockitoSpyBean` Examples
192
+
193
+ The following example shows how to use the default behavior of the `@MockitoSpyBean`
194
+ annotation:
195
+
196
+ [tabs]
197
+ ======
198
+ Java::
199
+ +
200
+ [source,java,indent=0,subs="verbatim,quotes"]
201
+ ----
202
+ @SpringJUnitConfig(TestConfig.class)
203
+ class BeanOverrideTests {
204
+
118
205
@MockitoSpyBean // <1>
119
206
CustomService customService;
120
207
121
- // test case body ...
208
+ // tests ...
122
209
}
123
210
----
124
211
<1> Wrap the bean with type `CustomService` with a Mockito `spy`.
@@ -137,12 +224,13 @@ Java::
137
224
+
138
225
[source,java,indent=0,subs="verbatim,quotes"]
139
226
----
140
- class OverrideBeanTests {
227
+ @SpringJUnitConfig(TestConfig.class)
228
+ class BeanOverrideTests {
229
+
141
230
@MockitoSpyBean("service") // <1>
142
231
CustomService customService;
143
232
144
- // test case body...
145
-
233
+ // tests...
146
234
}
147
235
----
148
236
<1> Wrap the bean named `service` with a Mockito `spy`.
0 commit comments