Class MockBean<T>

java.lang.Object
org.jboss.weld.junit.MockBean<T>
Type Parameters:
T - See also WeldInitiator.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
  • Constructor Details

  • Method Details

    • builder

      public static <T> MockBean.Builder<T> builder()
      By default, the bean:
      • has no name
      • has Dependent scope
      • has Any qualifier and Default 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) or MockBean.Builder.create(CreateFunction) must be always set. Otherwise, an IllegalStateException is thrown during MockBean.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 a Bean with default values (see also builder()). Additionaly, the specified bean types are added to the set of bean types and Contextual.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 a MockBean.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 interface jakarta.enterprise.context.spi.Contextual<T>
    • destroy

      public void destroy(T instance, jakarta.enterprise.context.spi.CreationalContext<T> creationalContext)
      Specified by:
      destroy in interface jakarta.enterprise.context.spi.Contextual<T>
    • getBeanClass

      public Class<?> getBeanClass()
      Specified by:
      getBeanClass in interface jakarta.enterprise.inject.spi.Bean<T>
    • getInjectionPoints

      public Set<jakarta.enterprise.inject.spi.InjectionPoint> getInjectionPoints()
      Specified by:
      getInjectionPoints in interface jakarta.enterprise.inject.spi.Bean<T>
    • getTypes

      public Set<Type> getTypes()
      Specified by:
      getTypes in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • getQualifiers

      public Set<Annotation> getQualifiers()
      Specified by:
      getQualifiers in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • getScope

      public Class<? extends Annotation> getScope()
      Specified by:
      getScope in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • getName

      public String getName()
      Specified by:
      getName in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • getStereotypes

      public Set<Class<? extends Annotation>> getStereotypes()
      Specified by:
      getStereotypes in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • isAlternative

      public boolean isAlternative()
      Specified by:
      isAlternative in interface jakarta.enterprise.inject.spi.BeanAttributes<T>
    • getId

      public String getId()
      Specified by:
      getId in interface jakarta.enterprise.inject.spi.PassivationCapable