42¢ glxn.net

Using Arquillian to test against a remote jboss container from within IDEA

I recently got introduced to Arquillian. Arquillian is a JBoss project for providing better testing to the java ee stack. Arquillian utilizes ShrinkWrap, another great project, for creating virtual archives as deployments to the container. It’s all pretty damn slick :) I encourage you to take a dive into the community. It’s well organized and alive.

In this post I’m going to look at running Arquillian against jboss as 6 remotely from within Intellij IDEA.

First off I have generated a project using the minimal Java EE Weld archetype. The Weld Archvetype will in the near future have support for generating the Arquillian part of the project as well.

To get the project into IDEA is really simple, just choose ‘open project’ and select the pom of the generated maven project. IDEA will then take a couple of minutes setting things up.

For Arquillian magic to work, we need to add junit (or you could use testng if you prefer), and the arquillian dependency to the pom.

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.8.1</version>
	<scope>test</scope>
</dependency>

<dependency>
	<groupId>org.jboss.arquillian</groupId>
	<artifactId>arquillian-junit</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<scope>test</scope>
</dependency>

Then we add a profile to the pom for running the tests against the remote jboss container.

<profiles>
	<profile>
		<id>jbossas-remote-60</id>
		<dependencies>
			<dependency>
				<groupId>org.jboss.arquillian.container</groupId>
				<artifactId>arquillian-jbossas-remote-60</artifactId>
				<version>1.0.0-SNAPSHOT</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</profile>
</profiles>

Notice that I have set the scope on the profile to test. The reason for this is that i use IDEA to deploy the project to the server (not maven), and I don’t want to toggle the profile in maven plugin all the time. If the profile is activated when you do a redeploy, then IDEA will(rightfully so) think you want these dependencies deployed as part of the project and deployment will fail.

So, let’s have some fun.

For my test case I have created a very simple Bean:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@RequestScoped
@Named
public class HelloAction {

    private String input;
    private String output;

    public void say() {
        output = "You said: " + input;
		System.out.println(output);
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }
}

I want to deploy this bean to the container, and test it. The Following test case does the trick:

import org.jboss.arquillian.api.Deployment;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Archives;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

@RunWith(org.jboss.arquillian.junit.Arquillian.class)
public class HelloTestCase {
   @Deployment
   public static Archive<?> createDeployment() {
            JavaArchive archive = Archives.create("test.jar", JavaArchive.class)
                 .addClasses(HelloAction.class)
                 .addManifestResource(new ByteArrayAsset("<beans></beans>".getBytes()),
                                 ArchivePaths.create("beans.xml"));

            return archive;
   }

   @Inject
   HelloAction helloAction;


   @Test
   public void helloBeanShouldSayHello() {
       helloAction.setInput("hello");
       helloAction.say();
       Assert.assertEquals("You said: hello", helloAction.getOutput());
   }
}

The @Deployment method describes the virtual archive I want deployed to the container. Here I create a simple archive and add the class I want to test. For CDI beans to work you will have to add the beans.xml to the archive.

We also need to add jndi.properties file to the project. This is needed for Arquillian to be able to connect to the remote container. Make sure that java.naming.provider.url reflects the IP of your remote server instance. Place jndi.properties file under src/test/resources:

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

Now we simply run the test from within IDEA using right click > run test, or ctrl+shift+f10. Just remember that this is a test running against a remote jboss container, so jboss needs to be running ;)

We see the test runs green:

And that the server has the expected output in the log:

Just for kicks, let’s break the test by changing the input to yo

TIP: If you want to debug your test, you have to remember that your code is running in the remote container. Running the test in debug mode will not work. You will have to start the container in debug mode to do this.

See the Arquillian FAQ page for more tips.

comments powered by Disqus

Moar stuffs

12 Sep 2017 Building an executable WS client using maven and metro
07 Jun 2015 Deploy an Ember app to gh-pages using npm run-script
06 Jun 2015 JSON Contract testing using unit tests to assert full stack integration across REST services
03 May 2015 simple http serve a directory from terminal
07 Jan 2014 civu, a CLI for cloning git repositories from jenkins views
06 Jan 2014 PyramidSort, a Sublime Text plugin for for reformatting text
05 Jan 2014 Git commit-message hook for JIRA issue tags
31 May 2013 hacking kitchen tiles with coffeescript
30 May 2013 Nuke, ps grep kill something
24 May 2013 mvnr: recursive mvn command runner
23 May 2013 Query By Example for JPA
22 May 2013 gitr: recursive git command runner
21 May 2013 Keeping gh-pages branch in sync with master
19 May 2013 Migrated from wordpress to jekyll and github pages
14 Aug 2012 Using Sublime Text 2 as git commit message editor
10 Mar 2012 QRGen, a small wrapper on top of ZXING for generating QRCodes in java
04 Jan 2012 My Bash PS1 with git branch info
17 Aug 2010 Making a swing project using IntelliJ IDEA GUI builder with maven, Including executable jar
01 May 2010 Using Arquillian to test against a remote jboss container from within IDEA
06 Apr 2010 WELD/CDI lightningtalk from Know IT 2010 annual conference
03 Apr 2010 Solving Sudoku using java swing and junit
01 Mar 2010 Simple CDI/WELD login example
01 Mar 2010 Implementing @RequestParam in CDI/WELD using Qualifier and InjectionPoint as @HttpParam
01 Nov 2009 Seam Maven Refimpl