1
1
[[spring-testing-annotation-beanoverriding-mockitobean]]
2
2
= `@MockitoBean` and `@MockitoSpyBean`
3
3
4
- `@MockitoBean` and `@MockitoSpyBean` are used on test class fields to override beans in
5
- the test's `ApplicationContext` with a Mockito mock or spy, respectively. In the latter
6
- case, the original bean definition is not replaced, but instead an early instance of the
7
- bean is captured and wrapped by the spy.
4
+ `@MockitoBean` and `@MockitoSpyBean` are used on fields in test classes to override beans
5
+ 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.
8
8
9
- By default, the annotated field's type is used to search for candidate definitions to
10
- override. If multiple candidates match, the usual `@Qualifier` can be provided to
11
- narrow the candidate to override. Alternatively, a candidate whose bean definition name
12
- matches the name of the field will match.
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.
13
13
14
14
To use a by-name override rather than a by-type override, specify the `name` attribute
15
15
of the annotation.
16
16
17
17
[WARNING]
18
18
====
19
- The qualifiers , including the name of the field are used to determine if a separate
20
- `ApplicationContext` needs to be created. If you are using this feature to mock or
21
- spy the same bean in several tests, make sure to name the field consistently to avoid
19
+ Qualifiers , including the name of the field, are used to determine if a separate
20
+ `ApplicationContext` needs to be created. If you are using this feature to mock or spy
21
+ the same bean in several tests, make sure to name the field consistently to avoid
22
22
creating unnecessary contexts.
23
23
====
24
24
25
25
Each annotation also defines Mockito-specific attributes to fine-tune the mocking details.
26
26
27
27
The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION`
28
28
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
29
-
30
- If no definition matches, then a definition is created on-the-fly.
29
+ If no existing bean definition matches, a new bean definition is created on the fly.
31
30
32
31
The `@MockitoSpyBean` annotation uses the `WRAP_BEAN`
33
32
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy],
34
- and the original instance is wrapped in a Mockito spy.
35
-
36
- It requires that exactly one candidate definition exists.
33
+ and the original instance is wrapped in a Mockito spy. This strategy requires that
34
+ exactly one candidate bean definition exists.
37
35
38
36
The following example shows how to use the default behavior of the `@MockitoBean` annotation:
39
37
@@ -44,20 +42,20 @@ Java::
44
42
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
45
43
----
46
44
class OverrideBeanTests {
47
- @MockitoBean // <1>
48
- private CustomService customService;
45
+ @MockitoBean // <1>
46
+ CustomService customService;
49
47
50
48
// test case body...
51
49
}
52
50
----
53
51
<1> Replace the bean with type `CustomService` with a Mockito `mock`.
54
52
======
55
53
56
- In the example above, we are creating a mock for `CustomService`. If more that
57
- one bean with such type exist , the bean named `customService` is considered. Otherwise,
58
- the test will fail and you will need to provide a qualifier of some sort to identify which
59
- of the `CustomService` beans you want to override. If no such bean exists, a bean
60
- definition will be created with an auto-generated bean name.
54
+ In the example above, we are creating a mock for `CustomService`. If more than one bean
55
+ of that type exists , the bean named `customService` is considered. Otherwise, the test
56
+ will fail, and you will need to provide a qualifier of some sort to identify which of the
57
+ `CustomService` beans you want to override. If no such bean exists, a bean definition
58
+ will be created with an auto-generated bean name.
61
59
62
60
The following example uses a by-name lookup, rather than a by-type lookup:
63
61
@@ -68,14 +66,14 @@ Java::
68
66
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
69
67
----
70
68
class OverrideBeanTests {
71
- @MockitoBean(name = "service") // <1>
72
- private CustomService customService;
69
+ @MockitoBean(name = "service") // <1>
70
+ CustomService customService;
73
71
74
72
// test case body...
75
73
76
74
}
77
75
----
78
- <1> Replace the bean named `service` with a Mockito `mock`.
76
+ <1> Replace the bean named `service` with a Mockito `mock`.
79
77
======
80
78
81
79
If no bean definition named `service` exists, one is created.
@@ -89,19 +87,19 @@ Java::
89
87
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
90
88
----
91
89
class OverrideBeanTests {
92
- @MockitoSpyBean // <1>
93
- private CustomService customService;
90
+ @MockitoSpyBean // <1>
91
+ CustomService customService;
94
92
95
93
// test case body...
96
94
}
97
95
----
98
96
<1> Wrap the bean with type `CustomService` with a Mockito `spy`.
99
97
======
100
98
101
- In the example above, we are wrapping the bean with type `CustomService`. If more that
102
- one bean with such type exist , the bean named `customService` is considered. Otherwise,
103
- the test will fail and you will need to provide a qualifier of some sort to identify which
104
- of the `CustomService` beans you want to spy.
99
+ In the example above, we are wrapping the bean with type `CustomService`. If more than
100
+ one bean of that type exists , the bean named `customService` is considered. Otherwise,
101
+ the test will fail, and you will need to provide a qualifier of some sort to identify
102
+ which of the `CustomService` beans you want to spy.
105
103
106
104
The following example uses a by-name lookup, rather than a by-type lookup:
107
105
@@ -112,12 +110,12 @@ Java::
112
110
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
113
111
----
114
112
class OverrideBeanTests {
115
- @MockitoSpyBean(name = "service") // <1>
116
- private CustomService customService;
113
+ @MockitoSpyBean(name = "service") // <1>
114
+ CustomService customService;
117
115
118
116
// test case body...
119
117
120
118
}
121
119
----
122
- <1> Wrap the bean named `service` with a Mockito `spy`.
120
+ <1> Wrap the bean named `service` with a Mockito `spy`.
123
121
======
0 commit comments