Annotation Interface TestBean


@Target(FIELD) @Retention(RUNTIME) @Documented public @interface TestBean
Mark a field to override a bean definition in the 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. To explicitly specify a bean name to replace, set 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

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Name of a static factory method to look for in the test class, which will be used to instantiate the bean to override.
    Name of the bean to override.
    Alias for name().
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static 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

      static final String 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

      @AliasFor("name") String value
      Alias for name().

      Intended to be used when no other attributes are needed — for example, @TestBean("customBeanName").

      See Also:
      Default:
      ""
    • name

      @AliasFor("value") String 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 methodName
      Name 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:
      ""