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 custom
Bean implementation is useful for mocking.
A new instance is usually created through a MockBean.Builder (see also builder()) and then passed to the
WeldInitiator.Builder#addBeans(Bean...) method.
- Since:
- 1.1
- Author:
- Martin Kouba
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder instance should not be reused nor shared.static interfacestatic interface -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMockBean(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
Modifier and TypeMethodDescriptionstatic <T> MockBean.Builder<T>builder()By default, the bean: has no name hasDependentscope hasAnyqualifier andDefaultis added automatically if no other qualifiers are set hasObjectbean type has no stereotypes is not an alternativevoidClass<?>getId()Set<jakarta.enterprise.inject.spi.InjectionPoint>getName()Class<? extends Annotation>getScope()Set<Class<? extends Annotation>>getTypes()booleanstatic <T> jakarta.enterprise.inject.spi.Bean<T>A convenient method to create aBeanwith default values (see alsobuilder()).static <T> MockBean.Builder<T>A convenient method to create aMockBean.Builderinitialized from the specified bean class.
-
Constructor Details
-
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 Details
-
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
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
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
- Specified by:
createin interfacejakarta.enterprise.context.spi.Contextual<T>
-
destroy
public void destroy(T instance, jakarta.enterprise.context.spi.CreationalContext<T> creationalContext) - Specified by:
destroyin interfacejakarta.enterprise.context.spi.Contextual<T>
-
getBeanClass
- Specified by:
getBeanClassin interfacejakarta.enterprise.inject.spi.Bean<T>
-
getInjectionPoints
- Specified by:
getInjectionPointsin interfacejakarta.enterprise.inject.spi.Bean<T>
-
getTypes
- Specified by:
getTypesin interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getQualifiers
- Specified by:
getQualifiersin interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getScope
- Specified by:
getScopein interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getName
- Specified by:
getNamein interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getStereotypes
- Specified by:
getStereotypesin interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
isAlternative
public boolean isAlternative()- Specified by:
isAlternativein interfacejakarta.enterprise.inject.spi.BeanAttributes<T>
-
getId
- Specified by:
getIdin interfacejakarta.enterprise.inject.spi.PassivationCapable
-