To test a EE application you have several solutions. One of them is to use EJBContainer.
EJBContainer container = EJBContainer.createEJBContainer(properties);
// do test
These steps are often in a test lifecycle (@BeforeClass/@AfterClass for JUnit for instance).
With OpenEJB you also have:
This allows you to get injections (@Inject, @Resource, @PersistenceContext…) in the test class.
That’s nice but you can make it easier!
OpenEJB ApplicationComposer is an old but very efficient way to test EE code. The idea is almost the same as with ShrinkWrap and Arquillian but with a big difference: it is performance oriented and embedded only. As a quick reminder to use the ApplicationComposer (either the JUnit runner or the abstract TestNG class) you just define a test class with a set of @Module methods declaring the in memory EE model (WebApp for web.xml, Beans for beans.xml, EjbJar for openejb-jar.xml, @Classes for scanned classes…) and standard @Test methods. Of course injections work in the test class. For instance:
When depending on a FTP in production we often simulate it through a
local file in dev and sometimes in preprod. That’s great and avoid to
need a big infra in all environment but make ftp dependencies optional
before the prod…so you risk to clean them and miss them in prod.
To avoid it it is easy to test FTP through JUnit using MockFtp project.
Some days ago I wrote a post on how to test a JBatch batch more easily making it synchronous (+ few other features). BatchEE proposal goes today one step further allowing to test steps!
Arquillian is a great way to deploy an application in a container to test it. However the framework itself doesn’t know if the container is local or remote. TomEE adapter (the bridge between arquillian and tomee) was supporting local deployment but remote one was quite hard and needed to workaround a bit arquillian which was not that fancy.
Since yesterday you can do it easily!
Recently BatchEE (Apache implementation of JBatch based on the Reference Implementation) got a test module. This is still “experimental” and can change but here is the first proposal.
Arquillian is great but some apps would prefer to use maven failsafe plugin to do their integration tests. If all your tests are client tests you can simply start the container and deploy the app before the tests are executed (pre-integration-test phase) and undeploy/stop the container after the test (post-integration-test phase). If it is not you can still write a servlet to execute the tests on the server side…wait, you are redoing Arquillian so maybe something is wrong in this case ;).
It is generally complicated/long to configure because you need to find how to start your container then to deploy an application on it etc…boring.
The solution is quite easy if you want to test against TomEE: the tomee maven plugin of course!