Class WeldInitiator

All Implemented Interfaces:
Instance<Object>, Provider<Object>, Iterable<Object>, ContainerInstance, org.junit.rules.TestRule

public class WeldInitiator extends AbstractWeldInitiator implements org.junit.rules.TestRule
JUnit 4 initiator - test rule which starts a Weld container per each test method execution:
 import org.junit.Rule;

 public class SimpleTest {

     @Rule
     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());
     }
 }
 

Alternatively, the container can be shared accross all test methods:

 import org.junit.ClassRule;

 public class ClassRuleTest {

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

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

     @Test
     public void test2() {
         // This test method is using the same Weld container as test1()
     }
 }
 

WeldInitiator implements Instance and therefore might be used to perform programmatic lookup of bean instances.

Author:
Martin Kouba, 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 test rule
      See Also:
    • of

      public static WeldInitiator of(Weld weld)
      The container is configured through a provided Weld instance.
      Parameters:
      weld -
      Returns:
      a new test rule
    • 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 test rule
    • 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 test rule
    • from

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

      public static WeldInitiator.Builder from(Weld weld)
      Create a builder instance.
      Parameters:
      weld -
      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:
    • getTestClassInjectorRule

      public org.junit.rules.MethodRule getTestClassInjectorRule()
      Returns a MethodRule that can be used as a Rule to inject into the test class instance. When using this Weld initiator as Rule, the same can be achieved by calling inject(this) on the builder. But when using this Weld initiator as ClassRule, there is no this reference available. In that case this method can be used as Rule to do the test class injection part.

      Example:

      
       @ClassRule
       public static WeldInitiator weld = WeldInitiator.from(Foo.class).build();
      
       @Rule
       public MethodRule testClassInjectorRule = weld.getTestClassInjectorRule();
       
      Returns:
      the test class injector rule
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule