Friday, April 18, 2014

simple cdi and ejb tests with deltaspike

apache deltaspike v0.6+ provides an easy to use, but quite powerful junit test-runner. it's based on the container-control api of deltaspike. deltaspike itself currently provides implementations for openwebeans, weld and openejb (tomee).

the last one is important, if you need to test a cdi+ejb based application. just by using a different container-control implementation (deltaspike-cdictrl-openejb) in your build config and adding openejb (/tomee) via org.apache.openejb:openejb-core, you can test cdi-beans as well as ejbs. just inject them as usual via @Inject (also @EJB works as expected).

@RunWith(CdiTestRunner.class)
public class InjectionTest
{
@Inject
private MyCdiBean bean1;
@EJB //or @Inject
private MyEjb bean2;
@Test
public void injection()
{
Assert.assertNotNull(this.bean1);
Assert.assertNotNull(this.bean2);
//...
}
}
an example can be found here.