Package org.jboss.weld.junit4
Class WeldInitiator
- java.lang.Object
-
- org.jboss.weld.junit.AbstractWeldInitiator
-
- org.jboss.weld.junit4.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() } }WeldInitiatorimplementsInstanceand therefore might be used to perform programmatic lookup of bean instances.- Author:
- Martin Kouba, Matej Novotny
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classWeldInitiator.BuilderThis builder can be used to customize the finalWeldInitiatorinstance, e.g.-
Nested classes/interfaces inherited from class org.jboss.weld.junit.AbstractWeldInitiator
AbstractWeldInitiator.AbstractBuilder<I extends AbstractWeldInitiator,T extends AbstractWeldInitiator.AbstractBuilder<I,T>>, AbstractWeldInitiator.ToInject
-
Nested classes/interfaces inherited from interface jakarta.enterprise.inject.Instance
Instance.Handle<T extends Object>
-
-
Field Summary
-
Fields inherited from class org.jboss.weld.junit.AbstractWeldInitiator
beans, container, extension, instancesToInject, scopesToActivate, weld
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)static WeldcreateWeld()The returnedWeldinstance has: automatic discovery disabled concurrent deployment disabledstatic WeldInitiator.Builderfrom(Class<?>... beanClasses)Create a builder instance.static WeldInitiator.Builderfrom(Weld weld)Create a builder instance.static WeldInitiator.BuilderfromTestPackage()Create a builder instance.org.junit.rules.MethodRulegetTestClassInjectorRule()Returns aMethodRulethat can be used as aRuleto inject into the test class instance.static WeldInitiatorof(Class<?>... beanClasses)The container is configured with the result ofcreateWeld()method and the given bean classes are added.static WeldInitiatorof(Weld weld)The container is configured through a providedWeldinstance.static WeldInitiatorofTestPackage()The container is configured with the result ofcreateWeld()method and all the classes from the test class package are added.static WeldInitiatorperformDefaultDiscovery()The container is instructed to do automatic bean discovery, the resulting bean archive is NOT synthetic.-
Methods inherited from class org.jboss.weld.junit.AbstractWeldInitiator
container, createToInject, destroy, event, get, getBeanManager, getHandle, getId, handles, initWeldContainer, injectInstances, injectNonContextual, isAmbiguous, isRunning, isUnsatisfied, iterator, releaseInstances, select, select, select, shutdown, shutdownWeldContainer
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jakarta.enterprise.inject.Instance
handlesStream, isResolvable, stream
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
of
public static WeldInitiator of(Class<?>... beanClasses)
The container is configured with the result ofcreateWeld()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:
Weld.beanClasses(Class...)
-
of
public static WeldInitiator of(Weld weld)
The container is configured through a providedWeldinstance.- Parameters:
weld-- Returns:
- a new test rule
-
ofTestPackage
public static WeldInitiator ofTestPackage()
The container is configured with the result ofcreateWeld()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 toWeldInitiator.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:
of(Class...)
-
from
public static WeldInitiator.Builder from(Weld weld)
Create a builder instance.- Parameters:
weld-- Returns:
- a builder instance
- See Also:
of(Weld)
-
fromTestPackage
public static WeldInitiator.Builder fromTestPackage()
Create a builder instance.- Returns:
- a builder instance
- See Also:
ofTestPackage()
-
createWeld
public static Weld createWeld()
The returnedWeldinstance has:- automatic discovery disabled
- concurrent deployment disabled
- Returns:
- a new
Weldinstance suitable for testing - See Also:
AbstractWeldInitiator.createWeld()
-
getTestClassInjectorRule
public org.junit.rules.MethodRule getTestClassInjectorRule()
Returns aMethodRulethat can be used as aRuleto inject into the test class instance. When using this Weld initiator asRule, the same can be achieved by callinginject(this)on the builder. But when using this Weld initiator asClassRule, there is nothisreference available. In that case this method can be used asRuleto 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:
applyin interfaceorg.junit.rules.TestRule
-
-