Class TestBeanContainer
- All Implemented Interfaces:
BeanContainer
BeanContainer interface dedicated to the unit tests implying and
requiring an IoC/IoD container. It plays like a simple smart stub of a true IoC container.
It provides additional methods allowing to register in the container the classes of beans or to
put directly into the container the beans to manage. If a class is put into the container, then
both the instantiation and its initialization is managed by the container. If a bean is
explicitly put into the container, then only its initialization is taken in charge by the
container. The initialization of a bean consists of its dependency resolution and of the
invocation of its PostConstruct annotated method (in case there is one). When the
container is cleared, an invocation of the @PreDestroy annotated method is invoked for
each bean before to be cleared (in case they have one). The dependency injection is done for all
fields annotated with either Inject or Resource
annotations. This implementation of the BeanContainer is built upon some assumptions to
be kept simple and usable in unit tests:
- Only one bean is managed for a given type and for all the parents of this type.
- Only the default constructor without parameters is supported. For classes with a constructor having the parameters to be resolved by the container, it is recommended to calls it explicitly in the test and to put the created instance directly into the container.
- The beans aren't proxied, meaning no resolution of the parameters is performed when a method of a managed bean is invoked. This has to be taken in charge by the test itself.
- The
Providerannotation isn't supported - Any specific IoC/IoD annotations and extensions aren't supported
- Author:
- mmoquillon
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the container.<T> Set<T> getAllBeansByType(Class<T> type, Annotation... qualifiers) <T> Optional<T> getBeanByName(String name) <T> Optional<T> getBeanByType(Class<T> type, Annotation... qualifiers) <T> voidputBean(Class<T> beanType, Class<? super T> type, Annotation... qualifiers) Asks to manage the specified class of beans for the given type and with the specified qualifiers.<T> voidAsks to manage the specified class of beans under the given name so that the beans could be retrieved later with that name.<T> voidputBean(T bean, Class<T> type, Annotation... qualifiers) Puts directly into the container the specified bean for the given type and with the specified qualifiers.<T> voidPuts directly into the container the specified bean and with the given name so that the bean could be retrieved later with that name.
-
Constructor Details
-
TestBeanContainer
public TestBeanContainer()
-
-
Method Details
-
getBeanByName
- Specified by:
getBeanByNamein interfaceBeanContainer
-
getBeanByType
- Specified by:
getBeanByTypein interfaceBeanContainer
-
getAllBeansByType
- Specified by:
getAllBeansByTypein interfaceBeanContainer
-
putBean
Puts directly into the container the specified bean and with the given name so that the bean could be retrieved later with that name.- Type Parameters:
T- the concrete type of the bean- Parameters:
bean- the bean to manage.name- the name inder which the bean will be put into the container.
-
putBean
Asks to manage the specified class of beans under the given name so that the beans could be retrieved later with that name.- Type Parameters:
T- the concrete type of the beans- Parameters:
beanType- the type of beans to manage.name- the name with which the beans will be created.
-
putBean
Asks to manage the specified class of beans for the given type and with the specified qualifiers.- Parameters:
beanType- the class of beans to put into the container at instantiation time. Only one bean will be created on demand.type- the type for which the beans will be created and retrieved later.qualifiers- zero, one or more annotations qualifying the beans put into the container in order to be retrieved later with these qualifiers.
-
putBean
Puts directly into the container the specified bean for the given type and with the specified qualifiers.- Type Parameters:
T- the concrete type of the bean.- Parameters:
bean- the bean to manage.type- the type for which the bean will be retrieved later.qualifiers- zero, one or more annotations qualifying the bean put into the container in order to be retrieved later with these qualifiers.
-
clear
public void clear()Clears the container. The @PreDestroyannotated method of each managed bean (if such a method exists) will be invoked before being wiped out.
-