@TestBean

@TestBean 用于测试类中的字段,用工厂方法提供的实例覆盖测试的 ApplicationContext 中的特定 bean。

@TestBean is used on a field in a test class to override a specific bean in the test’s ApplicationContext with an instance provided by a factory method.

关联的工厂方法名称派生自带注释的字段名称,或者如果指定,则使用 bean 名称。工厂方法必须为 static,不接受任何参数,并且具有与要覆盖的 bean 类型兼容的返回类型。为了更明确,或者如果你希望使用不同的名称,则注释允许提供特定方法名称。

The associated factory method name is derived from the annotated field’s name, or the bean name if specified. The factory method must be static, accept no arguments, and have a return type compatible with the type of the bean to override. To make things more explicit, or if you’d rather use a different name, the annotation allows for a specific method name to be provided.

默认情况下,带注释的字段的类型用于搜索要覆盖的候选 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 override the same bean in several tests, make sure to name the field consistently to avoid creating unnecessary contexts.

以下示例展示了如何使用 @TestBean 注释的默认行为:

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

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

	// test case body...

	private static CustomService customService() { (2)
		return new MyFakeCustomService();
	}
}
1 Mark a field for overriding the bean with type CustomService.
2 The result of this static method will be used as the instance and injected into the field.

在上面的示例中,我们用类型为 CustomService 的 bean 来覆盖 bean。如果存在多个该类型的 bean,则考虑名为 customService 的 bean。否则,测试将失败,并且你需要提供某种 限定符来标识要覆盖的 CustomService bean。

In the example above, we are overriding 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 override.

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

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

Java
class OverrideBeanTests {
	@TestBean(name = "service", methodName = "createCustomService") (1)
	private CustomService customService;

	// test case body...

	private static CustomService createCustomService() { (2)
		return new MyFakeCustomService();
	}
}
1 Mark a field for overriding the bean with name service, and specify that the factory method is named createCustomService.
2 The result of this static method will be used as the instance and injected into the field.

Spring 在测试类、测试类层次结构以及 @Nested 测试类的封闭类层次结构中搜索要调用的工厂方法。

Spring searches for the factory method to invoke in the test class, in the test class hierarchy, and in the enclosing class hierarchy for a @Nested test class.