Home > Java > Testing Glassfish Embedded in Eclipse without Maven

Testing Glassfish Embedded in Eclipse without Maven

Glassfish Embedded is a great thing, specially for test-driven developers, but there are a few things to know if you’re going to use it. In this small article I will show how to setup eclipse to use glassfish-embedded without Maven.

Note: Whethever you’re planning to use Junit with Maven or not, I suggest you to read this little blog article  for a fast overview and this one for war example usage.

First of all there are different set of api (and jars) depending on what you want to do:

  • javax.ejb.embeddable.EJBContainer
    It is a standard for all ejb container implementations/vendors and its api it’s very simple (sometimes enough).
  • org.glassfish.embeddable.*
    It’s the complete embedded implementation for glassfish for all types of JEE components.

Oracle has some useful documentation about all Apis and I use it as a reference more than use guide. I found more interesting blogs googling around for real examples.

Anyway in breaf:  if you need only ejbs then use the first. If you need other JEE components use the second.
There are many examples on the web for both cases but the aim of this article is about how to setup the environment and launch Junit Tests without maven.
The above APIs can be provided in different glassfish-embedded distribution depending on: you have a glassfish installation or not, you are using org.glassfish.embeddable.* or javax.ejb.embeddable.EJBContainer only. For simplicity I suggest to download the whole-in-one distibution: glassfish-embedded-all jar. Find out the latest version into maven repository http://download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-all/
I know, it’s huge (about 64MB) but I preferer having all apis available and up-to-date for development rather than realizing one day I must add or change my classpath because I d0n’t have it. I also don’t want my test project to heavily depend on local glassfish installation.

Setting up Test Project

An example of test project for glassfish embedded test

Usually unit tests comes within the project itself. This have pros and cons but I prefer having a test project apart whenever is possible/worth with.

Let’s say you have an ejb project, create a simple Java Project which depens on it (inside its Build Path configuration).

Note: ejb project is a GlassFish ejb project while test project is not.

Add the dowloaded glassfish-embcdded-all jar to the test project classpath.
Before start writing your Junit class, you must define your own glassfish embedded configuration. Oracle documentation says you can use an already existing domain.xml and I think this is the best approach: you can define configurations such as datasources in xml file rather that creating such resources programmatically!

Building up the tree structure glassfish expects to find simplifies the code so I suggest you to setup a new simple glassfish installation and copy the generated structure into your test project. Notice that you don’t have to copy jar files but just config files!
In our example, the config tree structure starts with “config/glassfish” directory.

Now you are ready to write your own test class. Initialization of embedded server can be achived like:

Map<String, Object> properties = new HashMap<String, Object>();
properties.put(“org.glassfish.ejb.embedded.glassfish.installation.root”,”../myejb-project-test/config/glassfish”);
properties.put(EJBContainer.MODULES, new File(“../myejb-project/build/classes”));
properties.put(EJBContainer.APP_NAME, “myejb-project”);
EJBContainer.createEJBContainer(properties);

Ones you have wrote your own test, if you try to lauch it with RunAs >Junit, you may encounter exceptions like:

java.net.BindException: No free port within range: 7676=com.sun.enterprise.v3.service.impl.ServiceIntitializerHandler

This is because test project inherits the GlassFish Container classpath of the ejb project. What you can do is going to run configuration of your junit test and exclude not exported classpath entries of the ejb project as shown here.

Pay attention that sometimes this setting seems not be applied the first time (I faced missing new settings just applied within Junit in Helios) but re-editing the rus-as configuration solves the problems.

Re running test now should work as expected.

Categorie:Java Tag:, ,
  1. Non c'è ancora nessun commento.
  1. agosto 26, 2015 alle 11:27
  2. ottobre 17, 2015 alle 04:10

Lascia un commento

Effettua il login con uno di questi metodi per inviare il tuo commento:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: