JBoss Unit Testing with Maven
Posted by billburke on February 13, 2008
Recently, I had the need to do some JBoss integration testing in a Maven environment. I had to piece together how to do this and couldn’t find one reference point that just gave me all the maven XML I had to cut/paste. So, I thought I’d log it here for others to reference. First of all, in your src/test/resources directory you need a jndi.properties file:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=jnp://localhost:1099
This isn’t used by any of the maven plugins i’m going to show you, but you will need it in your test code if you want to connect at all with JBoss. Next, here’s the build fragment of what you need in your pom.xml
<build>
<finalName>jsr311-war</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<jbossHome>/Users/billburke/jboss/jboss-4.2.2.GA</jbossHome>
</configuration>
<executions>
<execution>
<id>jboss-deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<fileName>${basedir}/target/jsr311-war.war</fileName>
</configuration>
</execution>
<execution>
<id>jboss-undeploy</id>
<phase>post-integration-test</phase>
<goals>
<goal>undeploy</goal>
</goals>
<configuration>
<fileName>${basedir}/target/jsr311-war.war</fileName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin> ...
</plugins>
</build>
First things first, is to use the Mojo JBoss plugin. This allows you to deploy/undeploy to JBoss. You must configure it to point to the JBoss home directory. I have it hardcoded, but you should use an environment variable. There are two executions that are run. One deploys a file I have in my projects target directory, the other undeploys it. These must run in the pre-integration-test phase and post-integration-test phase.
The next blob of XML deals with the Surefire test framework. Surefire will try to run tests before you have packaged and deployed your target archive. This is because in Maven’s lifecycle, testing comes before packaging and Surefire is automatically bound to run tests in the “test” phase. We turn this off and instead tell Surefire to run during the “integration-test” phase after our archive has been packaged and deployed to JBoss.
The mojo plugin also allows you to start and stop jboss. I could not get this to work with JBoss 4.2.2. Since I prefer to run JBoss manually when integration testing anyways, I was too lazy to investigate further.
February 14, 2008 at 8:12 pm
Great post! I was trying this at work today and couldn’t get jboss (4.2.2) started through the maven plugin either. Anyone who has (with 4.2.x)?
Anyways, I’ll try the deploying part tomorrow. However, getting the server started as well would be great for automatic maven builds. The cargo maven plugin should provide similar features, so possibly that could solve my needs (however, it lacks some configuration possibilities provided by jboss-maven-plugin).
Thanx,
/Anders
February 14, 2008 at 8:20 pm
Anders, please post back here if you find out anything on jboss start/stop plugins.
March 11, 2008 at 3:06 pm
Umm…that looks awful.
March 18, 2008 at 1:49 pm
After some struggling with the getting start/stop to work with the jboss-maven-plugin plugin, we turned to the cargo plugin for this.