Class AbstractWeldInitiator.AbstractBuilder<I extends AbstractWeldInitiator,T extends AbstractWeldInitiator.AbstractBuilder<I,T>>

java.lang.Object
org.jboss.weld.junit.AbstractWeldInitiator.AbstractBuilder<I,T>
Enclosing class:
AbstractWeldInitiator

protected abstract static class AbstractWeldInitiator.AbstractBuilder<I extends AbstractWeldInitiator,T extends AbstractWeldInitiator.AbstractBuilder<I,T>> extends Object
  • Field Details

    • weld

      protected final Weld weld
    • instancesToInject

      protected final List<Object> instancesToInject
    • scopesToActivate

      protected final Set<Class<? extends Annotation>> scopesToActivate
    • beans

      protected final Set<Bean<?>> beans
    • resources

      protected final Map<String,Object> resources
  • Constructor Details

    • AbstractBuilder

      public AbstractBuilder(Weld weld)
  • Method Details

    • activate

      @SafeVarargs public final T activate(Class<? extends Annotation>... normalScopes)
      Activate and deactivate contexts for the given normal scopes for the lifetime of the initialized Weld container, by default for each test method execution.

      ApplicationScoped is ignored as it is always active.

      Parameters:
      normalScopes -
      Returns:
      self
    • getEjbFactory

      protected Function<InjectionPoint,Object> getEjbFactory()
    • getPersistenceContextFactory

      protected Function<InjectionPoint,Object> getPersistenceContextFactory()
    • getPersistenceUnitFactory

      protected Function<InjectionPoint,Object> getPersistenceUnitFactory()
    • inject

      public T inject(Object instance)
      Instructs the initiator to inject the given non-contextual instance once the container is started, i.e. during test execution.

      This method could be used e.g. to inject a test class instance:

       public class InjectTest {
      
           @Rule
           public WeldInitiator weld = WeldInitiator.fromTestPackage().inject(this).build();
      
           @Inject
           Foo foo;
      
           @Test
           public void testFoo() {
               assertEquals("foo", foo.getId());
           }
       }
       

      Injected Dependent bean instances are destroyed after the test execution. However, the lifecycle of the non-contextual instance is not managed by the container and all injected references will be invalid after the test execution.

      Parameters:
      instance -
      Returns:
      self
    • addBeans

      public T addBeans(Bean<?>... beans)
      Instructs the initiator to add the specified beans during AfterBeanDiscovery notification.
      Parameters:
      beans -
      Returns:
      self
      Since:
      1.1
      See Also:
    • bindResource

      public T bindResource(String name, Object resource)
      Binds a name to an object. This allows to mock Resource injection points easily, e.g.:
       @Dependent
       class Foo {
      
           @Resource(lookup = "bar")
           String bar;
       }
       
      Parameters:
      name -
      resource -
      Returns:
      self
      Since:
      1.2
    • setEjbFactory

      public T setEjbFactory(Function<InjectionPoint,Object> ejbFactory)
      Makes it possible to mock @EJB injection points.

      Note that for Weld 3 org.jboss.weld.module:weld-ejb dependency is also required.

      Parameters:
      ejbFactory -
      Returns:
      self
      Since:
      1.2
    • setPersistenceUnitFactory

      public T setPersistenceUnitFactory(Function<InjectionPoint,Object> persistenceUnitFactory)
      Makes it possible to mock PersistenceUnit injection points.
      Parameters:
      persistenceUnitFactory -
      Returns:
      self
      Since:
      1.2
    • setPersistenceContextFactory

      public T setPersistenceContextFactory(Function<InjectionPoint,Object> persistenceContextFactory)
      Makes it possible to mock PersistenceContext injection points.
      Parameters:
      persistenceContextFactory -
      Returns:
      self
      Since:
      1.2
    • self

      protected abstract T self()
    • build

      protected abstract I build(Weld weld, List<Object> instancesToInject, Set<Class<? extends Annotation>> scopesToActivate, Set<Bean<?>> beans)
    • build

      public I build()
      Returns:
      a new initiator instance