For a multi-module JEE7 project, I wanted to be able to have a WildFly setup which is ready for both integration testing using Arquillian and regular integration testing (REST client and Selenium).
To be able to test JEE code, Arquillian is a suitable option (the only I know of). This runs the tests in container. I did not manage to get the embedded container working with WildFly, so the Arquillian tests use the same WildFly version as the traditional integration tests.
From maven, using cargo is handy when dealing with application servers. This can install the server you want and it can run your deployed application, including waiting for the application to fully load.
The build was organized in a way to assure only one WildFly copy was built and configured and that copy is used for the tests in all modules and for for tests in the IDE.
Then configuration is entirely done in the parent pom for the project. Only modules which do client testing (using Selenium, REST tests or other) need the following configration (these need to be war or ear modules):
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> </plugin> |
This allows you to use “mvn cargo:run” on that module and also assures the container with the module deployed is running while doing integration tests.
We need quite a bit of configuration in the parent pom.
For starters, some properties:
<properties> <wildfly.home.parent>${project.basedir}/.cargo/extracts/wildfly-8.0.0.CR1/wildfly-8.0.0.CR1</wildfly.home.parent> <wildfly.home.module>${project.basedir}/../.cargo/extracts/wildfly-8.0.0.CR1/wildfly-8.0.0.CR1</wildfly.home.module> <jboss.home>${wildfly.home.module}</jboss.home> <postgresql.version>9.3-1100-jdbc41</postgresql.version> <wildfly.version>8.0.0.CR1</wildfly.version> </properties> |
We configure two locations where WildFly can be found, one to be used from child modules (one level down the directory tree), and one relative with the parent pom. We also configure an alias for the module path, partly because it us shorter, partly because the Arquillian WildFly container expects a jbossHome variable.
Some variables need to be known when the tests are running, so configure the following plugins:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <argLine>${argLine} -Xmx1124m -Xms256m</argLine> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <jboss.home>${jboss.home}</jboss.home> <module.path>${jboss.home}/modules</module.path> </systemPropertyVariables> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.16</version> <configuration> <argLine>${argLine} -Xmx1024m -Xms256m</argLine> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <jboss.home>${jboss.home}</jboss.home> <module.path>${jboss.home}/modules</module.path> </systemPropertyVariables> </configuration> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> </execution> </executions> </plugin> |
Now we need to configure cargo for starting and stopping the container on integration tests (in pluginManagement):
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.6</version> <configuration> <container> <containerId>wildfly8x</containerId> <log>${basedir}/target/cargo.log</log> <output>${basedir}/target/wildfly.log</output> <zipUrlInstaller> <url>http://download.jboss.org/wildfly/8.0.0.CR1/wildfly-8.0.0.CR1.zip</url> <downloadDir>${project.basedir}/../.cargo/downloads</downloadDir> <extractDir>${project.basedir}/../.cargo/extracts</extractDir> </zipUrlInstaller> </container> <configuration> <properties> <cargo.servlet.port>8080</cargo.servlet.port> <cargo.logging>medium</cargo.logging> <cargo.jvmargs>${argLine} -Denv=test -Dtapestry.execution-mode=development</cargo.jvmargs> <cargo.jboss.configuration>standalone-full</cargo.jboss.configuration> </properties> </configuration> </configuration> <executions> <execution> <id>start-cargo</id> <phase>pre-integration-test</phase> <goals><goal>start</goal></goals> <configuration> <deployables> <deployable> <!-- Use exploded deploy: override location to point to the exploded webapp. --> <location>${project.basedir}/target/${project.artifactId}-${project.version}</location> <pingURL>http://localhost:8080/myApp</pingURL> <pingTimeout>120000</pingTimeout> <!-- 2 min --> <properties> <context>myApp</context> </properties> </deployable> </deployables> </configuration> </execution> <execution> <id>stop-cargo</id> <phase>post-integration-test</phase> <goals><goal>stop</goal></goals> </execution> </executions> </plugin> |
This indicates that cargo needs to be started and stopped before and after integration tests. It also indicates where WildFly can be found.
To make this work, we need to assure the installed WildFly is preconfigured. In my case I need a fix to allow httpclient to be used and I need to assure the correct data source is installed (both the driver jar and the configuration itself). This can be done using the command:
mvn -Dsetup process-test-resources |
The configuration is put in a profile to assure it is not included in every build. As the .cargo is not cleared when running “mvn clean” the configuration is long lasting.
<profile> <!-- setup wildfly container for use by cargo --> <id>setup-cargo</id> <activation> <property> <name>setup</name> </property> </activation> <build> <plugins> <plugin> <inherited>false</inherited> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>wildfly8x</containerId> <zipUrlInstaller> <url>http://download.jboss.org/wildfly/8.0.0.CR1/wildfly-8.0.0.CR1.zip</url> <downloadDir>${project.basedir}/.cargo/downloads</downloadDir> <extractDir>${project.basedir}/.cargo/extracts</extractDir> </zipUrlInstaller> </container> </configuration> <executions> <execution> <id>install-cargo</id> <phase>process-test-resources</phase> <goals><goal>install</goal></goals> </execution> </executions> </plugin> <plugin> <inherited>false</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-db-driver</id> <phase>process-test-resources</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> <outputDirectory>${wildfly.home.parent}/standalone/deployments</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <inherited>false</inherited> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources-ds</id> <phase>process-test-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${wildfly.home.parent}/standalone/deployments</outputDirectory> <resources> <resource> <directory>etc/wildfly/deploy/test</directory> <filtering>false</filtering> </resource> </resources> </configuration> </execution> <execution> <id>copy-resources-wfly-2634</id> <phase>process-test-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${wildfly.home.parent}/modules/system/layers/base/org/jboss/resteasy/resteasy-jaxrs/main</outputDirectory> <resources> <resource> <directory>etc/wildfly/deploy/WFLY-2634</directory> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> |
To finish, your arquillian.xml needs to be configured correctly.
<?xml version="1.0" encoding="UTF-8"?> <arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> <defaultProtocol type="Servlet 3.0" /> <container qualifier="jboss" default="true"> <configuration> <property name="jbossHome">${jboss.home}</property> <property name="serverConfig">standalone-full.xml</property> <!-- uncomment below for debugging <property name="javaVmArguments">-Xmx512m -XX:MaxPermSize=128m -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8787</property> --> </configuration> </container> </arquillian> |
Using the maven build always uses the correct WildFly installation and my IDE (IntelliJ IDEA) allows me to run individual Arquillian tests.
Leave a Reply