@ -25,29 +25,63 @@ import java.lang.annotation.Target;
@@ -25,29 +25,63 @@ import java.lang.annotation.Target;
import org.springframework.test.bean.override.BeanOverride ;
/ * *
* Mark a field to represent a "method" bean override of the bean of the same
* name and inject the field with the overriding instance .
* Mark a field to override a bean instance in the { @code BeanFactory } .
*
* < p > The instance is created from a static method in the declaring class which
* return type is compatible with the annotated field and which name follows the
* convention :
* < p > The instance is created from a no - arg static method in the declaring
* class whose return type is compatible with the annotated field . The method
* is deduced as follows :
* < ul >
* < li > if the annotation ' s { @link # methodName ( ) } is specified ,
* look for that one . < / li >
* < li > if not , look for exactly one method named with the
* { @link # CONVENTION_SUFFIX } suffix and either : < / li >
* < ul >
* < li > starting with the annotated field name < / li >
* < li > starting with the bean name < / li >
* < / ul >
* < li > if the { @link # methodName ( ) } is specified , look for a static method with
* that name . < / li >
* < li > if not , look for exactly one static method named with a suffix equal to
* { @value # CONVENTION_SUFFIX } and either starting with the annotated field
* name , or starting with the bean name . < / li >
* < / ul >
*
* < p > The annotated field ' s name is interpreted to be the name of the original
* bean to override , unless the annotation ' s { @link # name ( ) } is specified .
* < p > Consider the following example :
*
* < pre > < code >
* class CustomerServiceTests {
*
* & # 064 ; TestBean
* private CustomerRepository repository ;
*
* // Tests
*
* private static CustomerRepository repositoryTestOverride ( ) {
* return new TestCustomerRepository ( ) ;
* }
* } < / code > < / pre >
*
* < p > In the example above , the { @code repository } bean is replaced by the
* instance generated by the { @code repositoryTestOverride } method . Not only
* the overridden instance is injected in the { @code repository } field , but it
* is also replaced in the { @code BeanFactory } so that other injection points
* for that bean use the override .
*
* < p > To make things more explicit , the method name can be set , as shown in the
* following example :
*
* < pre > < code >
* class CustomerServiceTests {
*
* & # 064 ; TestBean ( methodName = "createTestCustomerRepository" )
* private CustomerRepository repository ;
*
* // Tests
*
* private static CustomerRepository createTestCustomerRepository ( ) {
* return new TestCustomerRepository ( ) ;
* }
* } < / code > < / pre >
*
* < p > By default , the name of the bean is inferred from the name of the annotated
* field . To use a different bean name , set the { @link # name ( ) } property .
*
* @see TestBeanOverrideProcessor
* @author Simon Baslé
* @author Stephane Nicoll
* @since 6 . 2
* @see TestBeanOverrideProcessor
* /
@Target ( ElementType . FIELD )
@Retention ( RetentionPolicy . RUNTIME )
@ -56,23 +90,23 @@ import org.springframework.test.bean.override.BeanOverride;
@@ -56,23 +90,23 @@ import org.springframework.test.bean.override.BeanOverride;
public @interface TestBean {
/ * *
* The method suffix expected as a convention in static methods which
* provides an override instance .
* Required suffix for a method that overrides a bean instance that is
* detected by convention .
* /
String CONVENTION_SUFFIX = "TestOverride" ;
/ * *
* The name of a static method to look for in the Configuration , which will
* be used to instantiate the override bean and inject the annotated field .
* < p > Default is { @code "" } ( the empty String ) , which is translated into
* the annotated field ' s name concatenated with the
* { @link # CONVENTION_SUFFIX } .
* Name of a static method to look for in the test , which will be used to
* instantiate the bean to override .
* < p > Default to { @code "" } ( the empty String ) , which detects the method
* to us by convention .
* /
String methodName ( ) default "" ;
/ * *
* The name of the original bean to override , or { @code "" } ( the empty
* String ) to deduce the name from the annotated field .
* Name of the bean to override .
* < p > Default to { @code "" } ( the empty String ) to use the name of the
* annotated field .
* /
String name ( ) default "" ;
}