Annotation Interface TestBean


@Target(FIELD) @Retention(RUNTIME) @Documented @BeanOverride(org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor.class) public @interface TestBean
@TestBean is an annotation that can be applied to a field in a test class to override a bean in the test's ApplicationContext using a static factory method.

By default, the bean to override is inferred from the type of the annotated field. If multiple candidates exist, 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 fallback qualifier. Alternatively, you can explicitly specify a bean name to replace by setting the value or name attribute.

A new bean definition will be created if a corresponding bean definition does not exist. However, if you would like for the test to fail when a corresponding bean definition does not exist, you can set the enforceOverride attribute to true.

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;

     // @Test methods ...

     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")
     CustomerRepository repository;

     // @Test methods ...

     static CustomerRepository createTestCustomerRepository() {
         return new TestCustomerRepository();
     }
 }

NOTE: Only singleton beans can be overridden. Any attempt to override a non-singleton bean will result in an exception.

Since:
6.2
Author:
Simon Baslé, Stephane Nicoll, Sam Brannen
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether to require the existence of a bean definition for the bean being overridden.
    Name of the static factory method that will be used to instantiate the bean to override.
    Name of the bean to override.
    Alias for name().
  • 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, taking qualifiers into account if necessary. See the class-level documentation for details.

      See Also:
      Default:
      ""
    • methodName

      String methodName
      Name 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:
      ""
    • enforceOverride

      boolean enforceOverride
      Whether to require the existence of a bean definition for the bean being overridden.

      Defaults to false which means that a new bean definition will be created if a corresponding bean definition does not exist.

      Set to true to cause an exception to be thrown if a corresponding bean definition does not exist.

      See Also:
      Default:
      false