Annotation Type TestManagedBean


@Target({FIELD,PARAMETER,METHOD}) @Retention(RUNTIME) @Documented public @interface TestManagedBean
This annotation is used to indicate that the type of a field or a parameter of a test (method) in the unit test class has to be managed by the IoC container used by the unit tests. If the type is annotated with the Named qualifier, it will be also registered as being managed under this specified name. The field or the parameter is then valued with an expected managed bean before executing any unit test. For doing, the bean type must have a default constructor. Once instantiated, the dependencies on others managed beans will be resolved and if the bean has a PostConstruct annotated method, then this method will be invoked. This annotation is for classes for which a true instance is preferred to a mock in a unit test.

The field annotated with this annotation can be explicitly instantiated, in that case the instance will be put directly into the IoC container. The dependencies of the instance on others managed bean will be then resolved and the PostConstruct annotated method, if any, will be invoked. If the test class declares several annotated fields having a common type among their ancestor, then the type of the fields will be registered for that type and their value could be get by using the TestBeanContainer.getAllBeansByType(Class, Annotation...) method. In this 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 registered as to be managed by the IoC container and finally the parameter is valued with resulting managed bean. By using this annotation with the parameters, you can get any previously registered bean.

Author:
mmoquillon