Web Application – Integration test with Arquillian

Integration test with Arquillian

Arqullian is an infrastucture for doing integration tests by creating a reproduction archive of the source web app included of the unit test.

It integrates either with JUnit and TestNG.

This is the mission of Arquillian:

  • managing the lifecycle of the container (start/stop),
  • bundling the test class with dependent classes and resources into a deployable archive,
  • enhancing the test class (e.g., resolving @Inject, @EJB and @Resource injections),
  • deploying the archive to test (deploy/undeploy) and
  • capturing results and failures.

Basically, once the developer create the test archive using ShrinkWrap class, Arquillian automatically deal with it by deploying the package, run the test and optionally starting / stopping the container.

Arquillian basic example

You can download the Arquillian Example code HERE

Looking at documentation or maven archetypes, you’ll see examples like the following:

This code is maybe good for showing Arquillian usage, but not for real world, where your web app could be multi-module composed with a many, many dependencies.

Arquillian real world example

In this chapter we’ll create a Rest Integration Test archive that contains ALL the module dependencies automatically imported by the Maven resolver.

First off, create an empty ear archive using maven archetype; for this purpose we’ll create a compliant jboss 6.4 ear module.

Then you can create a simple EJB that return “hello” word.

Now open the web module, edit pom.xml and add these dependencies:

So have added:

  • shrinkwrap artifact and arquillian modules;
  • a logging library
  • resteasy library (compliant with JBoss spec)

We can now include a class JaxRsActivator that extends jax rs Application for initialiting our rest methods.

And our rest implementation entity

Despite is not – obviously – a real world example what we really need to know if how to create an ear test module without the need of importing all classes and dependencies manually

Under maven test package, create a class named ArqTest.java with the following lines:

Since ShrinkWrap doesn’t support EJB archive actually¬† (at least with 2.0.0 version) what we need is:

  1. Create an EAR archive
  2. Import EJB dependencies with Maven resolver
  3. Create an EJB package and add all needed classes (even resolving these by package)
  4. Add imported EJB dependencies as resources within the EJB archive
  5. Create war (don’t forget to create beans.xml both in war end ejb archive in order to make injection work)

We have also created a test-application.xml file, which is needed to conformly create our ear archive

For the test part, @RunAsClient annotation is used because we want these test to run OUTSIDE the container.

Notice also that @ArquillianResource is useful because automatically resolve our deployed ear test URL (which may be even a remote URL by properly configure the arquillian.xml file).

Arquillian configuration

The following is an example of arquillian.xml properly configured for running a test on a local or remote container, even with managed option

If you are willing to create the archive for the first time, it’s also very important to see the built package for verifying the result, so setting deploymentExportPath will produce the output in the selected path.

Run the Arquillian integration test

If you created the project using a maven archetype like written on the first part of this article, you’ll notice that pom.xml has already some arquillian profiles created.

If you already started your local JBoss environment, simply launch this command:

If you need to test your web application in a AS remote address, type the following:

Download example

You can download the Arquillian Example code HERE