Class WeldInitiator

All Implemented Interfaces:
Instance<Object>, Provider<Object>, Iterable<Object>, ContainerInstance

public class WeldInitiator extends AbstractWeldInitiator
JUnit 5 initiator - can be used to customize the Weld SE container started by WeldJunit5Extension.

Example:

 @ExtendWith(WeldJunit5Extension.class)
 public class SimpleTest {

     @WeldSetup
     public WeldInitiator weld = WeldInitiator.of(Foo.class);

     @Test
     public void testFoo() {
         // Weld container is started automatically
         // WeldInitiator can be used to perform programmatic lookup of beans
         assertEquals("baz", weld.select(Foo.class).get().getBaz());
     }
 }
 
Author:
Matej Novotny
  • Method Details

    • of

      public static WeldInitiator of(Class<?>... beanClasses)
      The container is configured with the result of createWeld() method and the given bean classes are added. If any of added classes is an extension, it is automatically recognized and enabled.
      Parameters:
      beanClasses -
      Returns:
      a new WeldInitiator instance
      See Also:
    • of

      public static WeldInitiator of(Weld weld)
      The container is configured through a provided Weld instance.

      Provided instance of Weld should be kept in the same scope as the resulting WeldInitiator. I.e. if Weld instance is kept in a static field, so should be the resulting WeldInitiator. Failing to uphold this can lead to repetitive configuration of the same Weld instance which in turn results in unexpected container setup/behavior.

      Parameters:
      weld - instance of Weld used to create WeldInitiator
      Returns:
      a new WeldInitiator instance
    • ofTestPackage

      public static WeldInitiator ofTestPackage()
      The container is configured with the result of createWeld() method and all the classes from the test class package are added.
      Returns:
      a new WeldInitiator instance
    • performDefaultDiscovery

      public static WeldInitiator performDefaultDiscovery()
      The container is instructed to do automatic bean discovery, the resulting bean archive is NOT synthetic. Note that this requires beans.xml to be present. It is equals to WeldInitiator.of(new Weld()) invocation.
      Returns:
      a new WeldInitiator instance
    • from

      public static WeldInitiator.Builder from(Class<?>... beanClasses)
      Create a builder instance.
      Parameters:
      beanClasses -
      Returns:
      a builder instance
      See Also:
    • from

      public static WeldInitiator.Builder from(Weld weld)
      Create a builder instance.

      Provided instance of Weld should be kept in the same scope as the resulting WeldInitiator. I.e. if Weld instance is kept in a static field, so should be the resulting WeldInitiator. Failing to uphold this can lead to repetitive configuration of the same Weld instance which in turn results in unexpected container setup/behavior.

      Parameters:
      weld - instance of Weld used as a basis for this WeldInitiator.Builder
      Returns:
      a builder instance
      See Also:
    • fromTestPackage

      public static WeldInitiator.Builder fromTestPackage()
      Create a builder instance.
      Returns:
      a builder instance
      See Also:
    • createWeld

      public static Weld createWeld()
      The returned Weld instance has:
      • automatic discovery disabled
      • concurrent deployment disabled
      Returns:
      a new Weld instance suitable for testing
      See Also: