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:
jakarta.enterprise.context.spi.Contextual<T>
,jakarta.enterprise.inject.spi.Bean<T>
,jakarta.enterprise.inject.spi.BeanAttributes<T>
,jakarta.enterprise.inject.spi.PassivationCapable
public class MockBean<T> extends Object implements jakarta.enterprise.inject.spi.Bean<T>, jakarta.enterprise.inject.spi.PassivationCapable
This customBean
implementation 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 class
MockBean.Builder<T>
A builder instance should not be reused nor shared.static interface
MockBean.CreateFunction<T>
static interface
MockBean.DestroyFunction<T>
-
Constructor Summary
Constructors Modifier Constructor Description 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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> MockBean.Builder<T>
builder()
By default, the bean: has no name hasDependent
scope hasAny
qualifier andDefault
is added automatically if no other qualifiers are set hasObject
bean type has no stereotypes is not an alternativeT
create(jakarta.enterprise.context.spi.CreationalContext<T> creationalContext)
void
destroy(T instance, jakarta.enterprise.context.spi.CreationalContext<T> creationalContext)
Class<?>
getBeanClass()
String
getId()
Set<jakarta.enterprise.inject.spi.InjectionPoint>
getInjectionPoints()
String
getName()
Set<Annotation>
getQualifiers()
Class<? extends Annotation>
getScope()
Set<Class<? extends Annotation>>
getStereotypes()
Set<Type>
getTypes()
boolean
isAlternative()
boolean
isNullable()
static <T> jakarta.enterprise.inject.spi.Bean<T>
of(T beanInstance, Type... beanTypes)
A convenient method to create aBean
with default values (see alsobuilder()
).static <T> MockBean.Builder<T>
read(Class<T> beanClass)
A convenient method to create aMockBean.Builder
initialized 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
Dependent
scope - has
Any
qualifier andDefault
is added automatically if no other qualifiers are set - has
Object
bean type - has no stereotypes
- is not an alternative
Note that
MockBean.Builder.creating(Object)
orMockBean.Builder.create(CreateFunction)
must be always set. Otherwise, anIllegalStateException
is thrown duringMockBean.Builder.build()
invocation.- Returns:
- a new builder instance
-
of
public static <T> jakarta.enterprise.inject.spi.Bean<T> of(T beanInstance, Type... beanTypes)
A convenient method to create aBean
with 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
MockBean
instance
-
read
public static <T> MockBean.Builder<T> read(Class<T> beanClass)
A convenient method to create aMockBean.Builder
initialized 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,
Unmanaged
is 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(jakarta.enterprise.context.spi.CreationalContext<T> creationalContext)
- Specified by:
create
in interfacejakarta.enterprise.context.spi.Contextual<T>
-
destroy
public void destroy(T instance, jakarta.enterprise.context.spi.CreationalContext<T> creationalContext)
- Specified by:
destroy
in interfacejakarta.enterprise.context.spi.Contextual<T>
-
getBeanClass
public Class<?> getBeanClass()
- Specified by:
getBeanClass
in interfacejakarta.enterprise.inject.spi.Bean<T>
-
getInjectionPoints
public Set<jakarta.enterprise.inject.spi.InjectionPoint> getInjectionPoints()
- Specified by:
getInjectionPoints
in interfacejakarta.enterprise.inject.spi.Bean<T>
-
isNullable
public boolean isNullable()
- Specified by:
isNullable
in interfacejakarta.enterprise.inject.spi.Bean<T>
-
getTypes
public Set<Type> getTypes()
- Specified by:
getTypes
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getQualifiers
public Set<Annotation> getQualifiers()
- Specified by:
getQualifiers
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getScope
public Class<? extends Annotation> getScope()
- Specified by:
getScope
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getName
public String getName()
- Specified by:
getName
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getStereotypes
public Set<Class<? extends Annotation>> getStereotypes()
- Specified by:
getStereotypes
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
isAlternative
public boolean isAlternative()
- Specified by:
isAlternative
in interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getId
public String getId()
- Specified by:
getId
in interfacejakarta.enterprise.inject.spi.PassivationCapable
-
-