Package org.jboss.weld.junit
Class MockBean<T>
- java.lang.Object
-
- org.jboss.weld.junit.MockBean<T>
-
- Type Parameters:
T- See alsoWeldInitiator.Builder#addBean(Bean)method.
- All Implemented Interfaces:
Contextual<T>,Bean<T>,BeanAttributes<T>,PassivationCapable
public class MockBean<T> extends Object implements Bean<T>, PassivationCapable
This customBeanimplementation is useful for mocking.A new instance is usually created through a
MockBean.Builder(see alsobuilder()) and then passed to theWeldInitiator.Builder#addBeans(Bean...)method.- Since:
- 1.1
- Author:
- Martin Kouba
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMockBean.Builder<T>A builder instance should not be reused nor shared.static interfaceMockBean.CreateFunction<T>static interfaceMockBean.DestroyFunction<T>
-
Constructor Summary
Constructors Modifier Constructor Description protectedMockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name, Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope, MockBean.CreateFunction<T> createCallback, MockBean.DestroyFunction<T> destroyCallback)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> MockBean.Builder<T>builder()Tcreate(CreationalContext<T> creationalContext)voiddestroy(T instance, CreationalContext<T> creationalContext)Class<?>getBeanClass()StringgetId()Set<InjectionPoint>getInjectionPoints()StringgetName()Set<Annotation>getQualifiers()Class<? extends Annotation>getScope()Set<Class<? extends Annotation>>getStereotypes()Set<Type>getTypes()booleanisAlternative()static <T> Bean<T>of(T beanInstance, Type... beanTypes)static <T> MockBean.Builder<T>read(Class<T> beanClass)A convenient method to create aMockBean.Builderinitialized from the specified bean class.
-
-
-
Constructor Detail
-
MockBean
protected MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name, Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope, MockBean.CreateFunction<T> createCallback, MockBean.DestroyFunction<T> destroyCallback)
-
-
Method Detail
-
builder
public static <T> MockBean.Builder<T> builder()
By default, the bean:- has no name
- has
Dependentscope - has
Anyqualifier andDefaultis added automatically if no other qualifiers are set - has
Objectbean type - has no stereotypes
- is not an alternative
Note that
MockBean.Builder.creating(Object)orMockBean.Builder.create(CreateFunction)must be always set. Otherwise, anIllegalStateExceptionis thrown duringMockBean.Builder.build()invocation.- Returns:
- a new builder instance
-
of
public static <T> Bean<T> of(T beanInstance, Type... beanTypes)
A convenient method to create aBeanwith default values (see alsobuilder()). Additionaly, the specified bean types are added to the set of bean types andContextual.create(CreationalContext)will always return the specified bean instance.- Parameters:
beanInstance-beanTypes-- Returns:
- a
MockBeaninstance
-
read
public static <T> MockBean.Builder<T> read(Class<T> beanClass)
A convenient method to create aMockBean.Builderinitialized from the specified bean class.Note that the container may not be started yet and so it is not possible to use CDI SPI. Instead, we try to simulate the default bean discovery.
By default,
Unmanagedis used to create/destroy the bean instance. However, it is possible to override this behavior.- Parameters:
beanClass-- Returns:
- a new builder instance initialized from the specified bean class
-
create
public T create(CreationalContext<T> creationalContext)
- Specified by:
createin interfaceContextual<T>
-
destroy
public void destroy(T instance, CreationalContext<T> creationalContext)
- Specified by:
destroyin interfaceContextual<T>
-
getBeanClass
public Class<?> getBeanClass()
- Specified by:
getBeanClassin interfaceBean<T>
-
getInjectionPoints
public Set<InjectionPoint> getInjectionPoints()
- Specified by:
getInjectionPointsin interfaceBean<T>
-
getTypes
public Set<Type> getTypes()
- Specified by:
getTypesin interfaceBeanAttributes<T>
-
getQualifiers
public Set<Annotation> getQualifiers()
- Specified by:
getQualifiersin interfaceBeanAttributes<T>
-
getScope
public Class<? extends Annotation> getScope()
- Specified by:
getScopein interfaceBeanAttributes<T>
-
getName
public String getName()
- Specified by:
getNamein interfaceBeanAttributes<T>
-
getStereotypes
public Set<Class<? extends Annotation>> getStereotypes()
- Specified by:
getStereotypesin interfaceBeanAttributes<T>
-
isAlternative
public boolean isAlternative()
- Specified by:
isAlternativein interfaceBeanAttributes<T>
-
getId
public String getId()
- Specified by:
getIdin interfacePassivationCapable
-
-