Annotation Type TestManagedBean


  • @Target({FIELD,PARAMETER,METHOD})
    @Retention(RUNTIME)
    @Documented
    public @interface TestManagedBean
    This annotation is used to indicate that a field or a parameter has to be instantiated and then registered into the bean container used in the test before executing that test. For doing, the bean type must have a default constructor. Once instantiated, if the bean has a PostConstruct annotated method, then this method will be invoked. This annotation is for classes for which an instance is preferred to a mock in unit tests. The difference between such beans and those annotated with TestedBean is that the injection points in the former aren't resolved; indeed, TestManagedBean annotated beans are considered just like mock but with a true instance (a kind of stub then).

    Any field annotated with this annotation can be explicitly instantiated, in that case it is that instance that will be registered into the bean container and no PostConstruct annotated method will be invoked. If the test class declares several annotated fields having a common type among their ancestor, then the fields will be registered for that type and they could be get by using the TestBeanContainer.getAllBeansByType(Class, Annotation...) method. In that case, the call of TestBeanContainer.getBeanByType(Class, Annotation...) method with that type as parameter will throw an exception.

    If the annotation is applied to a parameter, then a bean of the parameter type is first looking for in the bean container used in the tests. If no such bean is found, then the type is instantiated, the newly created bean is registered into the bean container and finally it is passed as parameter value. By using this annotation with the parameters, you can get any previously registered bean.

    The annotation can also be applied to a method. In that case, the method has to return an array of concrete classes for which an instance has to be constructed and then registered into the bean container used in the test. It is then like applying the annotation on several fields but in this case, the instances are anonymous and aren't injected into the instance of the test class. Useful for tests requiring for their execution that some injections points have to be resolved in some dependencies of the tested bean without having to use explicitly those managed beans in the test class itself.

    Author:
    mmoquillon