|
|
|
@ -1,24 +1,24 @@ |
|
|
|
[[spring-testing-annotation-beanoverriding-mockitobean]] |
|
|
|
[[spring-testing-annotation-beanoverriding-mockitobean]] |
|
|
|
= `@MockitoBean` and `@MockitoSpyBean` |
|
|
|
= `@MockitoBean` and `@MockitoSpyBean` |
|
|
|
|
|
|
|
|
|
|
|
`@MockitoBean` and `@MockitoSpyBean` are used on test class fields to override beans in |
|
|
|
`@MockitoBean` and `@MockitoSpyBean` are used on fields in test classes to override beans |
|
|
|
the test's `ApplicationContext` with a Mockito mock or spy, respectively. In the latter |
|
|
|
in the test's `ApplicationContext` with a Mockito mock or spy, respectively. In the |
|
|
|
case, the original bean definition is not replaced, but instead an early instance of the |
|
|
|
latter case, the original bean definition is not replaced, but instead an early instance |
|
|
|
bean is captured and wrapped by the spy. |
|
|
|
of the bean is captured and wrapped by the spy. |
|
|
|
|
|
|
|
|
|
|
|
By default, the annotated field's type is used to search for candidate definitions to |
|
|
|
By default, the annotated field's type is used to search for candidate bean definitions |
|
|
|
override. If multiple candidates match, the usual `@Qualifier` can be provided to |
|
|
|
to override. If multiple candidates match, `@Qualifier` can be provided to narrow the |
|
|
|
narrow the candidate to override. Alternatively, a candidate whose bean definition name |
|
|
|
candidate to override. Alternatively, a candidate whose bean definition name matches the |
|
|
|
matches the name of the field will match. |
|
|
|
name of the field will match. |
|
|
|
|
|
|
|
|
|
|
|
To use a by-name override rather than a by-type override, specify the `name` attribute |
|
|
|
To use a by-name override rather than a by-type override, specify the `name` attribute |
|
|
|
of the annotation. |
|
|
|
of the annotation. |
|
|
|
|
|
|
|
|
|
|
|
[WARNING] |
|
|
|
[WARNING] |
|
|
|
==== |
|
|
|
==== |
|
|
|
The qualifiers, including the name of the field are used to determine if a separate |
|
|
|
Qualifiers, including the name of the field, are used to determine if a separate |
|
|
|
`ApplicationContext` needs to be created. If you are using this feature to mock or |
|
|
|
`ApplicationContext` needs to be created. If you are using this feature to mock or spy |
|
|
|
spy the same bean in several tests, make sure to name the field consistently to avoid |
|
|
|
the same bean in several tests, make sure to name the field consistently to avoid |
|
|
|
creating unnecessary contexts. |
|
|
|
creating unnecessary contexts. |
|
|
|
==== |
|
|
|
==== |
|
|
|
|
|
|
|
|
|
|
|
@ -26,14 +26,12 @@ Each annotation also defines Mockito-specific attributes to fine-tune the mockin |
|
|
|
|
|
|
|
|
|
|
|
The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION` |
|
|
|
The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION` |
|
|
|
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding]. |
|
|
|
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding]. |
|
|
|
|
|
|
|
If no existing bean definition matches, a new bean definition is created on the fly. |
|
|
|
If no definition matches, then a definition is created on-the-fly. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The `@MockitoSpyBean` annotation uses the `WRAP_BEAN` |
|
|
|
The `@MockitoSpyBean` annotation uses the `WRAP_BEAN` |
|
|
|
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy], |
|
|
|
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy], |
|
|
|
and the original instance is wrapped in a Mockito spy. |
|
|
|
and the original instance is wrapped in a Mockito spy. This strategy requires that |
|
|
|
|
|
|
|
exactly one candidate bean definition exists. |
|
|
|
It requires that exactly one candidate definition exists. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following example shows how to use the default behavior of the `@MockitoBean` annotation: |
|
|
|
The following example shows how to use the default behavior of the `@MockitoBean` annotation: |
|
|
|
|
|
|
|
|
|
|
|
@ -44,8 +42,8 @@ Java:: |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
class OverrideBeanTests { |
|
|
|
class OverrideBeanTests { |
|
|
|
@MockitoBean // <1> |
|
|
|
@MockitoBean // <1> |
|
|
|
private CustomService customService; |
|
|
|
CustomService customService; |
|
|
|
|
|
|
|
|
|
|
|
// test case body... |
|
|
|
// test case body... |
|
|
|
} |
|
|
|
} |
|
|
|
@ -53,11 +51,11 @@ Java:: |
|
|
|
<1> Replace the bean with type `CustomService` with a Mockito `mock`. |
|
|
|
<1> Replace the bean with type `CustomService` with a Mockito `mock`. |
|
|
|
====== |
|
|
|
====== |
|
|
|
|
|
|
|
|
|
|
|
In the example above, we are creating a mock for `CustomService`. If more that |
|
|
|
In the example above, we are creating a mock for `CustomService`. If more than one bean |
|
|
|
one bean with such type exist, the bean named `customService` is considered. Otherwise, |
|
|
|
of that type exists, the bean named `customService` is considered. Otherwise, the test |
|
|
|
the test will fail and you will need to provide a qualifier of some sort to identify which |
|
|
|
will fail, and you will need to provide a qualifier of some sort to identify which of the |
|
|
|
of the `CustomService` beans you want to override. If no such bean exists, a bean |
|
|
|
`CustomService` beans you want to override. If no such bean exists, a bean definition |
|
|
|
definition will be created with an auto-generated bean name. |
|
|
|
will be created with an auto-generated bean name. |
|
|
|
|
|
|
|
|
|
|
|
The following example uses a by-name lookup, rather than a by-type lookup: |
|
|
|
The following example uses a by-name lookup, rather than a by-type lookup: |
|
|
|
|
|
|
|
|
|
|
|
@ -68,14 +66,14 @@ Java:: |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
class OverrideBeanTests { |
|
|
|
class OverrideBeanTests { |
|
|
|
@MockitoBean(name = "service") // <1> |
|
|
|
@MockitoBean(name = "service") // <1> |
|
|
|
private CustomService customService; |
|
|
|
CustomService customService; |
|
|
|
|
|
|
|
|
|
|
|
// test case body... |
|
|
|
// test case body... |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
<1> Replace the bean named `service` with a Mockito `mock`. |
|
|
|
<1> Replace the bean named `service` with a Mockito `mock`. |
|
|
|
====== |
|
|
|
====== |
|
|
|
|
|
|
|
|
|
|
|
If no bean definition named `service` exists, one is created. |
|
|
|
If no bean definition named `service` exists, one is created. |
|
|
|
@ -89,8 +87,8 @@ Java:: |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
class OverrideBeanTests { |
|
|
|
class OverrideBeanTests { |
|
|
|
@MockitoSpyBean // <1> |
|
|
|
@MockitoSpyBean // <1> |
|
|
|
private CustomService customService; |
|
|
|
CustomService customService; |
|
|
|
|
|
|
|
|
|
|
|
// test case body... |
|
|
|
// test case body... |
|
|
|
} |
|
|
|
} |
|
|
|
@ -98,10 +96,10 @@ Java:: |
|
|
|
<1> Wrap the bean with type `CustomService` with a Mockito `spy`. |
|
|
|
<1> Wrap the bean with type `CustomService` with a Mockito `spy`. |
|
|
|
====== |
|
|
|
====== |
|
|
|
|
|
|
|
|
|
|
|
In the example above, we are wrapping the bean with type `CustomService`. If more that |
|
|
|
In the example above, we are wrapping the bean with type `CustomService`. If more than |
|
|
|
one bean with such type exist, the bean named `customService` is considered. Otherwise, |
|
|
|
one bean of that type exists, the bean named `customService` is considered. Otherwise, |
|
|
|
the test will fail and you will need to provide a qualifier of some sort to identify which |
|
|
|
the test will fail, and you will need to provide a qualifier of some sort to identify |
|
|
|
of the `CustomService` beans you want to spy. |
|
|
|
which of the `CustomService` beans you want to spy. |
|
|
|
|
|
|
|
|
|
|
|
The following example uses a by-name lookup, rather than a by-type lookup: |
|
|
|
The following example uses a by-name lookup, rather than a by-type lookup: |
|
|
|
|
|
|
|
|
|
|
|
@ -112,12 +110,12 @@ Java:: |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
class OverrideBeanTests { |
|
|
|
class OverrideBeanTests { |
|
|
|
@MockitoSpyBean(name = "service") // <1> |
|
|
|
@MockitoSpyBean(name = "service") // <1> |
|
|
|
private CustomService customService; |
|
|
|
CustomService customService; |
|
|
|
|
|
|
|
|
|
|
|
// test case body... |
|
|
|
// test case body... |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
<1> Wrap the bean named `service` with a Mockito `spy`. |
|
|
|
<1> Wrap the bean named `service` with a Mockito `spy`. |
|
|
|
====== |
|
|
|
====== |
|
|
|
|