Skip to main content

Testable Java EE 7 Maven Archetype, using Arquillian (Tech Tip #34)

Posted by arungupta on July 1, 2014 at 8:03 AM PDT

There is a Maven archetype to create Java EE 7 application:

mvn --batch-mode -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee7 -DgroupId=org.javaee7.sample -DartifactId=javaee7-sample -Dpackage=org.javaee7.sample archetype:generate

It generates a simple "pom.xml" with Java EE 7 API . It does the job to get started with building the application. But how do you test this app ?

Of course, you write unit and integration tests. But how do you run these tests, especially in a container-independent manner ?

That's where Arquillian comes in!

Arquillian guides explain how to write real tests, but you still need to figure out Maven dependencies, create profiles, figure out container dependencies, and more. That's still too much work :)

Meet a new Maven archetype that generates a Java EE 7 app, with profiles pre-configured for WildFly and GlassFish.

mvn --batch-mode archetype:generate -DarchetypeGroupId=org.javaee-samples -DarchetypeArtifactId=javaee7-arquillian-archetype -DgroupId=org.samples.javaee7.arquillian -DartifactId=arquillian

The four profiles are:

  1. wildfly-remote-arquillian
  2. wildfly-managed-arquillian
  3. glassfish-remote-arquillian
  4. glassfish-embedded-arquillian

The first profile is the most natural to start with. It requires to download WildFly 8.1, unzip and start using ./bin/standalone.sh. Then you can run the test as:
mvn test -Pwildfly-remote-arquillian

to see the result as:

Running org.wildfly.samples.javaee7.arquillian.EmployeeResourceTest
Jun 29, 2014 9:32:57 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.0.Beta2
Jun 29, 2014 9:32:57 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.0.Beta2
Jun 29, 2014 9:32:57 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version (unknown)
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.137 sec

This is useful if tests need to be executed multiple times on the same WildFly instance.

The second profile is the easiest to start with, and does not require any manual downloading. Using the profile downloads WildFly (8.0.0 at this time) to Maven repository, installs it in the "target" directory, starts the server, deploys the WAR file, runs the test, and stops the server.

[INFO] --- maven-dependency-plugin:2.8:unpack (unpack) @ arquillian ---
[INFO] Configured Artifact: org.wildfly:wildfly-dist:8.0.0.Final:zip
Downloading: http://repo.maven.apache.org/maven2/org/wildfly/wildfly-dist/8.0.0.Final... http://repo.maven.apache.org/maven2/org/wildfly/wildfly-dist/8.0.0.Final... (151090 KB at 1684.3 KB/sec)
[INFO] Unpacking /Users/arungupta/.m2/repository/org/wildfly/wildfly-dist/8.0.0.Final/wildfly-dist-8.0.0.Final.zip to /Users/arungupta/workspaces/wildfly-samples/arquillian/target with includes "" and excludes ""

. . .

T E S T S
-------------------------------------------------------
Running org.wildfly.samples.javaee7.arquillian.EmployeeResourceTest
Jun 30, 2014 1:35:46 PM org.jboss.as.arquillian.container.managed.ManagedDeployableContainer startInternal
13:35:53,105 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-1) HV000001: Hibernate Validator 5.0.3.Final
13:35:53,176 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named EmployeeRepository in deployment unit deployment "3c82ea4b-5638-4902-8896-cbc08ed6895e.war" are as follows:
java:global/3c82ea4b-5638-4902-8896-cbc08ed6895e/EmployeeRepository!org.wildfly.samples.javaee7.arquillian.EmployeeRepository
java:app/3c82ea4b-5638-4902-8896-cbc08ed6895e/EmployeeRepository!org.wildfly.samples.javaee7.arquillian.EmployeeRepository
java:module/EmployeeRepository!org.wildfly.samples.javaee7.arquillian.EmployeeRepository

. . .

13:39:33,622 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017506: Undertow 1.0.0.Final stopping
13:39:33,639 INFO  [org.jboss.as] (MSC service thread 1-15) JBAS015950: WildFly 8.0.0.Final "WildFly" stopped in 22ms

Results :

Tests run: 6, Failures: 0, Errors: 0, Skipped: 0

"glassfish-remote-arquillian" profile is like "wildfly-remote-arquillian" where an instance of GlassFish is started externally and tests are run in the usual manner. This profile does not work at this moment because of ARQ-1596.

"glassfish-embedded-arquillian" is like "wildfly-managed-arquillian" where GlassFish container is downloaded transparently using the Maven dependencies, starts the container, deploys the app, runs the test, and stops the container.

Archetype source code is at: github.com/javaee-samples/javaee7-archetypes/tree/master/javaee7-archetype and the archetype is published at search.maven.org/#search%7Cga%7C1%7Cjavaee7-arquillian-archetype.

Many thanks to @aslakknutsen for publishing this archetype!

A complete working sample can be checked out from github.com/arun-gupta/wildfly-samples/tree/master/arquillian.

Let us know if you find this useful and how would you use it.

Related Topics >>