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 definition is
present in the application context. A @Qualifier
annotation can be
used to help disambiguate. 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, any enclosing class it might have is also considered. Similarly, in case the test class inherits from a base class the whole class hierarchy is considered. The 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 a suffix equal to "TestOverride" and starting 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 repositoryTestOverride() {
return new TestCustomerRepository();
}
}
In the example above, the repository
bean is replaced by the
instance generated by the repositoryTestOverride()
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 = "repository", 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
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Required suffix for the name of a factory method that overrides a bean instance when the factory method is detected by convention.
-
Field Details
-
CONVENTION_SUFFIX
Required suffix for the name of a factory method that overrides a bean instance when the factory method is detected by convention.- See Also:
-
-
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.
- See Also:
- Default:
- ""
-
methodName
String methodNameName of a static factory method to look for in the test class, which will be used to instantiate the bean to override.In the case of a nested test, any enclosing class it might have is also considered. Similarly, in case the test class inherits from a base class the whole class hierarchy is considered.
If left unspecified, the name of the factory method will be detected based on convention.
- See Also:
- Default:
- ""
-