11[[spring-testing-annotation-beanoverriding-mockitobean]]
22= `@MockitoBean` and `@MockitoSpyBean`
33
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.
88
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.
1313
1414To use a by-name override rather than a by-type override, specify the `name` attribute
1515of the annotation.
1616
1717[WARNING]
1818====
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
2222creating unnecessary contexts.
2323====
2424
2525Each annotation also defines Mockito-specific attributes to fine-tune the mocking details.
2626
2727The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION`
2828xref: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.
3130
3231The `@MockitoSpyBean` annotation uses the `WRAP_BEAN`
3332xref: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.
3735
3836The following example shows how to use the default behavior of the `@MockitoBean` annotation:
3937
@@ -44,20 +42,20 @@ Java::
4442[source,java,indent=0,subs="verbatim,quotes",role="primary"]
4543----
4644 class OverrideBeanTests {
47- @MockitoBean // <1>
48- private CustomService customService;
45+ @MockitoBean // <1>
46+ CustomService customService;
4947
5048 // test case body...
5149 }
5250----
5351<1> Replace the bean with type `CustomService` with a Mockito `mock`.
5452======
5553
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.
6159
6260The following example uses a by-name lookup, rather than a by-type lookup:
6361
@@ -68,14 +66,14 @@ Java::
6866[source,java,indent=0,subs="verbatim,quotes",role="primary"]
6967----
7068 class OverrideBeanTests {
71- @MockitoBean(name = "service") // <1>
72- private CustomService customService;
69+ @MockitoBean(name = "service") // <1>
70+ CustomService customService;
7371
7472 // test case body...
7573
7674 }
7775----
78- <1> Replace the bean named `service` with a Mockito `mock`.
76+ <1> Replace the bean named `service` with a Mockito `mock`.
7977======
8078
8179If no bean definition named `service` exists, one is created.
@@ -89,19 +87,19 @@ Java::
8987[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9088----
9189 class OverrideBeanTests {
92- @MockitoSpyBean // <1>
93- private CustomService customService;
90+ @MockitoSpyBean // <1>
91+ CustomService customService;
9492
9593 // test case body...
9694 }
9795----
9896<1> Wrap the bean with type `CustomService` with a Mockito `spy`.
9997======
10098
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.
105103
106104The following example uses a by-name lookup, rather than a by-type lookup:
107105
@@ -112,12 +110,12 @@ Java::
112110[source,java,indent=0,subs="verbatim,quotes",role="primary"]
113111----
114112 class OverrideBeanTests {
115- @MockitoSpyBean(name = "service") // <1>
116- private CustomService customService;
113+ @MockitoSpyBean(name = "service") // <1>
114+ CustomService customService;
117115
118116 // test case body...
119117
120118 }
121119----
122- <1> Wrap the bean named `service` with a Mockito `spy`.
120+ <1> Wrap the bean named `service` with a Mockito `spy`.
123121======
0 commit comments