July 27, 2012

Java EE 6 Annotations - JSR 299: Contexts and Dependency Injection - Part 2

In my previous blog http://magnus-k-karlsson.blogspot.se/2012/07/java-ee-6-annotations-jsr-299-contexts.html I wrote about the new CDI annotation in EE 6 and now you would probably want to test them. You can of course package your archive and deploy it to your a EE container, but that is clumsy, tedious and harder to debug. What you rather want to is to write a JUnit test cases and with that be able to debug/run it from your IDE. There are several way to do that, but two major solution are:

In this blog I will show how to do that with JBoss Arquillian, but Apache OpenEJB is also a good solution.

There is a rather good getting started guide at http://arquillian.org/guides/getting_started/, which I encourage you to read and here I will give you a shorthand version, but also give some more clarification about the Arquillian Maven dependency.

First add JBoss Maven User repository, which is described here, https://community.jboss.org/wiki/MavenGettingStarted-Users.

Then create an empty maven project. Modify your pom.xml accordingly

There are two importing thing to stress here. First we need to understand one imported design goal with Arquillian and that is it was meant to be conainer neutral, i.e. the Arquillian test should not be affected if you decided to run your EE test on a different EE Container. And this design goal is reflected in the  Arquillian Maven dependency module.

So with that in mind, the first dependency arquillian-weld-ee-embedded-1.1 is only an adapter and does not contain any real CDI implementation code, which is in this case the Weld implementation code.

The second imported thing to understand is that Weld does not support EJB or any other similar EE modules, it only supports CDI.

If you want more container support look at the following pom, https://raw.github.com/arquillian/arquillian-examples/master/arquillian-tutorial/pom-no-container-profiles.xml.

No comments: