Hi!
We wrote a small test stub for starting up CDI containers from unit tests.
Just add the following to your pom
<dependency>
<groupId>org.apache.openwebbeans.test</groupId>
<artifactId>cditest</artifactId>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans.test</groupId>
<artifactId>cditest-owb</artifactId>
</dependency>
the version of those artifacts is the same like the owb core you use (1.1.1).
I always use a base class for my tests like the following:
public abstract class ContainerTest {
protected static CdiTestContainer containerStarter;
protected static int containerRefCount = 0;
protected <T> T getBeanInstance(Class<T> clazz, Annotation... qualifiers) {
return containerStarter.getInstance(clazz, qualifiers);
}
public final void setUp() throws Exception {
containerRefCount++;
if (containerStarter == null) {
ProjectStageProducer.setProjectStage(runInProjectStage());
containerStarter = CdiTestContainerLoader.getCdiContainer();
containerStarter.bootContainer();
}
else {
cleanInstances();
}
}
@BeforeClass
public final void beforeClass() throws Exception {
setUp();
containerStarter.stopRequestScope();
containerStarter.startRequestScope();
}
@AfterMethod
public final void tearDown() throws Exception {
containerStarter.stopRequestScope();
containerStarter.startRequestScope();
containerRefCount--;
}
public final void cleanInstances() throws Exception {
containerStarter.stopContexts();
containerStarter.startContexts();
}
@AfterSuite
public void shutdownContainer() throws Exception {
if (containerStarter != null) {
containerStarter.shutdownContainer();
containerStarter = null;
}
}
}
All the refCount stuff is only needed for testNG. In junit you don't need that.
But in testNG class tests are not executed in order but depending on various
dependsOn rules.
Feel free to ping me us you need more info.
LieGrue,
strub
--- On Wed, 4/27/11, djheisterberg <[email protected]> wrote:
> From: djheisterberg <[email protected]>
> Subject: Re: Using OpenWebBeans CDI with Junit ?
> To: [email protected]
> Date: Wednesday, April 27, 2011, 11:27 PM
> Just wondering if any progress has
> been made on this issue. Like the OP,
> using LocalClient and bind("inject", this) just results in
> unsatisfied
> dependencies for any Inject point. Building the OWB
> context "by hand"
> works. This is with OpenEJB 4.0 and OWB 1.1.1.
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Using-OpenWebBeans-CDI-with-Junit-tp3000191p3479717.html
> Sent from the OpenEJB Dev mailing list archive at
> Nabble.com.
>