@MockitoBean and @MockitoSpyBean

在测试类的字段上使用 @MockitoBean@MockitoSpyBean 可以分别使用 Mockito 模拟或间谍覆盖测试的 ApplicationContext 中的 bean。在后者情况下,不会替换原始 bean 定义,而是会捕获并由间谍包装 bean 的一个早期实例。

@MockitoBean and @MockitoSpyBean are used on fields in test classes to override beans in the test’s ApplicationContext with a Mockito mock or spy, respectively. In the latter case, the original bean definition is not replaced, but instead an early instance of the bean is captured and wrapped by the spy.

默认情况下,带注释的字段的类型用于搜索要覆盖的候选 bean 定义。如果多个候选对象匹配,可以提供 @Qualifier 以缩小要覆盖的候选对象范围。或者,匹配字段名称的 bean 定义名称的候选对象将匹配。

By default, the annotated field’s type is used to search for candidate bean definitions to override. If multiple candidates match, @Qualifier can be provided to narrow the candidate to override. Alternatively, a candidate whose bean definition name matches the name of the field will match.

为了使用按名称覆盖而不是按类型覆盖,请指定注释的 name 属性。

To use a by-name override rather than a by-type override, specify the name attribute of the annotation.

限定符(包括字段名称)用于确定是否需要创建单独的 ApplicationContext。如果您使用此功能在多个测试中模拟或监视同一 bean,请务必始终如一地命名该字段,以避免创建不必要的上下文。

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 spy the same bean in several tests, make sure to name the field consistently to avoid creating unnecessary contexts.

每个注释还定义了 Mockito 特定的属性来微调模拟详细信息。

Each annotation also defines Mockito-specific attributes to fine-tune the mocking details.

@MockitoBean 注释使用 REPLACE_OR_CREATE_DEFINITION strategy for test bean overriding。如果不存在匹配的 bean 定义,则动态创建一个新的 bean 定义。

The @MockitoBean annotation uses the REPLACE_OR_CREATE_DEFINITION strategy for test bean overriding. If no existing bean definition matches, a new bean definition is created on the fly.

@MockitoSpyBean 注释使用 `WRAP_BEAN`strategy,原始实例包装在 Mockito 特工中。此策略要求存在正好一个候选 Bean 定义。

The @MockitoSpyBean annotation uses the WRAP_BEAN strategy, and the original instance is wrapped in a Mockito spy. This strategy requires that exactly one candidate bean definition exists.

以下示例演示了如何使用 @MockitoBean 注解的默认行为:

The following example shows how to use the default behavior of the @MockitoBean annotation:

Java
class OverrideBeanTests {
	@MockitoBean (1)
	private CustomService customService;

	// test case body...
}
1 Replace the bean with type CustomService with a Mockito mock.

在上面的示例中,我们为 CustomService 创建一个模拟。如果存在多个该类型的 bean,则会考虑名为 customService 的 bean。否则,测试将失败,您将需要提供某种限定符来识别要覆盖的 CustomService bean。如果不存在此类 bean,系统将使用自动生成的 bean 名称创建 bean 定义。

In the example above, we are creating a mock for CustomService. If more than 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 of the CustomService beans you want to override. If no such bean exists, a bean definition will be created with an auto-generated bean name.

以下示例使用按名称查找,而不是按类型查找:

The following example uses a by-name lookup, rather than a by-type lookup:

Java
class OverrideBeanTests {
	@MockitoBean(name = "service") (1)
	private CustomService customService;

	// test case body...

}
1 Replace the bean named service with a Mockito mock.

如果不存在名为 service 的 bean 定义,则创建该 bean 定义。

If no bean definition named service exists, one is created.

以下示例演示了如何使用 @MockitoSpyBean 注解的默认行为:

The following example shows how to use the default behavior of the @MockitoSpyBean annotation:

Java
class OverrideBeanTests {
	@MockitoSpyBean (1)
	private CustomService customService;

	// test case body...
}
1 Wrap the bean with type CustomService with a Mockito spy.

在上面的示例中,我们用类型 CustomService 封装 bean。如果存在多个该类型的 bean,则会考虑命名为 customService 的 bean。否则,测试会失败,并且您需要提供某种限定符来识别您要监视的 CustomService bean。

In the example above, we are wrapping the bean with type CustomService. If more than 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 of the CustomService beans you want to spy.

以下示例使用按名称查找,而不是按类型查找:

The following example uses a by-name lookup, rather than a by-type lookup:

Java
class OverrideBeanTests {
	@MockitoSpyBean(name = "service") (1)
	private CustomService customService;

	// test case body...

}
1 Wrap the bean named service with a Mockito spy.