Annotation Interface TestBean
BeanFactory
.
By default, the bean to override is inferred from the type of the
annotated field. This requires that exactly one matching bean definition is
present in the application context. A @Qualifier
annotation can be
used to help disambiguate. In the absence of a @Qualifier
annotation,
the name of the annotated field will be used as a qualifier. Alternatively,
you can explicitly specify a bean name to replace by setting the
value()
or name()
attribute.
The instance is created from a zero-argument static factory method in the
test class whose return type is compatible with the annotated field. In the
case of a nested test class, the enclosing class hierarchy is also searched.
Similarly, if the test class extends from a base class or implements any
interfaces, the entire type hierarchy is searched. Alternatively, a factory
method in an external class can be referenced via its fully-qualified method
name following the syntax <fully-qualified class name>#<method name>
— for example, "org.example.TestUtils#createCustomerRepository"
.
The factory method is deduced as follows.
- If the
methodName()
is specified, look for a static method with that name. - If a method name is not specified, look for exactly one static method named with either the name of the annotated field or the name of the bean (if specified).
Consider the following example.
class CustomerServiceTests {
@TestBean
private CustomerRepository repository;
// Tests
private static CustomerRepository repository() {
return new TestCustomerRepository();
}
}
In the example above, the repository
bean is replaced by the
instance generated by the repository()
method. Not only is the
overridden instance injected into the repository
field, but it is
also replaced in the BeanFactory
so that other injection points for
that bean use the overridden bean instance.
To make things more explicit, the bean and method names can be set, as shown in the following example.
class CustomerServiceTests {
@TestBean(name = "customerRepository", methodName = "createTestCustomerRepository")
private CustomerRepository repository;
// Tests
private static CustomerRepository createTestCustomerRepository() {
return new TestCustomerRepository();
}
}
- Since:
- 6.2
- Author:
- Simon Baslé, Stephane Nicoll, Sam Brannen
- See Also:
-
TestBeanOverrideProcessor
-
Optional Element Summary
-
Element Details
-
value
Alias forname()
.Intended to be used when no other attributes are needed — for example,
@TestBean("customBeanName")
.- See Also:
- Default:
- ""
-
name
Name of the bean to override.If left unspecified, the bean to override is selected according to the annotated field's type, taking qualifiers into account if necessary. See the class-level documentation for details.
- See Also:
- Default:
- ""
-
methodName
String methodNameName of the static factory method that will be used to instantiate the bean to override.A search will be performed to find the factory method in the test class, in one of its superclasses, or in any implemented interfaces. In the case of a nested test class, the enclosing class hierarchy will also be searched.
Alternatively, a factory method in an external class can be referenced via its fully-qualified method name following the syntax
<fully-qualified class name>#<method name>
— for example,"org.example.TestUtils#createCustomerRepository"
.If left unspecified, the name of the factory method will be detected based either on the name of the annotated field or the name of the bean.
- Default:
- ""
-