Annotation Interface TestBean
BeanFactory.
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.
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 method name can be set, as shown in the following example.
class CustomerServiceTests {
@TestBean(methodName = "createTestCustomerRepository")
private CustomerRepository repository;
// Tests
private static CustomerRepository createTestCustomerRepository() {
return new TestCustomerRepository();
}
}
By default, the name of the bean to override is inferred from the name of
the annotated field. To use a different bean name, set the value() or
name() attribute.
- Since:
- 6.2
- Author:
- Simon Baslé, Stephane Nicoll, Sam Brannen
- See Also:
-
TestBeanOverrideProcessor
-
Optional Element Summary
Optional Elements -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringRequired 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 name of the bean to override is the name of the annotated field. If specified, the field name is ignored.
- 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:
- ""
-