HOWTO: Run tests with Embedded JBoss

August 20, 2009 at 9:03 am 6 comments

I’ve needed to create an environment for running unit tests on EJB3 JBoss application this week. There were few GOTCHAs so for the benefit of future generations here is a simple recipe for running your unit tests on Embedded JBoss.

First a link to Embedded JBoss wiki page. You can find some information there. Also download the latest version (I used beta3.SP9) from there. We are going to use Maven to get the jars but you still need the configuration files from the download.

Ok, lets setup our maven dependencies.

First define repository if you don’t have it yet:

   <name>Jboss Repo</name>

Now add actual dependencies for embedded jboss:





Now lets create a test class. It will look just like your normal test class but you need to add the following method to start and deploy JBoss

 public static void startJboss() throws Exception {
   Bootstrap bootstrap = Bootstrap.getInstance();


Two things happen here. First, bootstrap() method launches Embedded JBoss with basic configuration. With standard JBoss it would be enough since it would automatically deploy all ears. In this case though, we need to explicitly tell it what to deploy – that’s what deployResourceBase() command does.
Now that you have Embedded JBoss starting and deploying before your tests, you can write any code that tests those beans. For example:

 public void testMe() throws NamingException {
   InitialContext context = new InitialContext(); 
   MyBeanLocal bean = (MyBeanLocal)context.lookup("MyBean/local");

And now the GOTCHAs.

First for JBoss to run it needs some configuration. Remember that zip you downloaded? Open it up, find bootstrap folder inside and then copy entire contents of that folder into test/resources folder of your maven module. That’s the basic configuration. If you want something additional, like your own datasource, you can add it into the test/resources/deploy folder as you would normally add it to deploy folder of standard JBoss.

We are not done yet. If you are running on Java 6 and the chances are that you are, it will still not work. You must add the following JVM property to your unit test configuration to force JVM to run in Java 5 compatible mode: -Dsun.lang.ClassLoader.allowArraySyntax=true

And the last thing –  if you are running from IDE you are probably deploying from exploded classes folder and not from packager jar/ear. If so, there is a good chance that Embedded JBoss will fail because it won’t have enough file handles to properly deploy your classes. To fix it on Linux use ‘ulimit -n <some large number>’ to increase the number of file handles. On windows look up your own solution.

And that’s it. You now have working Embedded JBoss test environment. Congratulations 🙂

public void testMe() throws NamingException {
InitialContext context = new InitialContext();
MyBeanLocal bean = (MyBeanLocal)context.lookup(“MyBean/local”);

Entry filed under: Java, Open Source, Recipies.

Is eye candy underrated? How to become a rock star developer?

6 Comments Add your own

  • 1. ALR  |  August 20, 2009 at 9:17 am

    Cool stuff.

    Do you have any startup times you could share?


    • 2. idevone  |  August 20, 2009 at 11:19 am

      I never measured times but it’s fast enough – few seconds at most – so extremely useful for running integration testing scenarios.

  • 3. James  |  August 24, 2009 at 4:52 am

    Hi Gregory. Great article.This would do really well at JavaLobby? If you’d like to repost there send me a mail and we can organise it.


  • 4. zaske  |  June 17, 2010 at 2:57 pm

    Can you please give the following info:
    1. Are you using EJB3?
    2. We are trying to test code that it supposed to run on jboss-eap-5.0 – will sp3.beta9 work with code that is compliant to jboss-eap-5.0?
    3. I encountered an error when trying to perform Bootstap.getInstance().bootstrap(); – ProperyConfigurator threw a NullPointerException as there is a line in the code that performs getClass(0.getClassLoader() and then uses a method of the class loader, but the returned class loader is null.
    4. I managed to write a workaround to this, and now it tells me that a class from Trove that is needed in AspectManager cannot be found (this class exists also in the embedded jboss jars)
    Any ideas?

    • 5. idevone  |  June 18, 2010 at 7:54 am

      Yes, it’s for EJB3 and it worked with code running on community edition 5.0. The EAP wasn’t available back then. I am not sure what’s the differences between them.
      Fortunately (:)) I don’t do any JBoss development right now so I can’t provide any more specific details.

  • 6. zaske  |  June 21, 2010 at 8:08 am

    More Info –
    1. I succeeded in working with JBoss embedded! In the end there was something wrong with the build path of my project.
    2. I noticed that @PostConstruct is not called -but overcame this problem.
    3. I have a lookup question – our code is running normally in the context of “abc” – for example – to lookup the bean in the example you provided I would have needed to lookup for “abc/MyBean/local” and not “MyBean/local” when looking up when jboss server is running, but when jboss embedded is running the lookup is “MyBean/local” – how can i add a context to jboss embedded execution?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


August 2009
« Apr   Sep »

Most Recent Posts

%d bloggers like this: