Thursday, 17 January 2013

JUnit testing EJBs in embedded Glassfish in Netbeans (the hard way)

Back to basics: Testing

On the face of things it was worth going back basic testing because my frustration turned to joy!

I am by nature very inquisitive and have sometimes been known to spend "far too much time not being productive" at work trying to solve problems that my employer might not consider worthy of my time.

A bit of background is needed here: for the last few years I have been employed as a software developer by a company that until recently didn't really know what this meant. Not a criticism of my employer just a statement of fact that the discipline of Software Engineering and Development was not relevant to them.

Now I find myself being "allowed" to think about things like Unit testing.. oh the joy!

So, time scrape the rust off my Unit testing skills. First stop a Netbeans tutorial which demonstrates using JUnit to test EJBs in an embedded Glassfish. As "middleware" is what I do* I quickly knocked the example together as per the instructions... damn and blast Murphy and his law!!
Yeap, everything except the testing worked.
Here is the Message of Doom

Invalid ejb jar [WebAppJUnit.jar]: it contains zero ejb. 
1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message-driven bean. 
2. EJB3+ entity beans (@Entity) are POJOs and please package them as library jar. 
3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (@Stateless, @Stateful, @MessageDriven, @Singleton), please check server.log to see whether the annotations were processed properly.

Apparently my EJBs are not being found. After spending several fruitless hours looking for a solution and not finding anything that helped me at all my curiosity took over.

What I found out

The Message of Doom had a clue: it was trying to load an EJB jar called WebAppJUnit.jar. Well the tutorial doesn't say create an Enterprise Application with associated EJB Module so an EJB jar ain't being built!

The tutorial gets you to create a POJO Stateless Session Bean which sits in your WAR file.
This WAR file has to be loaded by the EJB Container in order to have @Annotations processed. This wasn't happening.

The tutorial mentions adding properties to further setup the EJBContainer but doesn't explain what the properties mean. Here it uses EJBContainer.MODULES with a file object. This property allows you to extend the Classpath of the EJB Container, something I found out during my fruitless googling.

In order to get the tutorial working you simply add the following lines of code

        Map properties = new HashMap();
        properties.put(EJBContainer.MODULES, new File("dist/WebAppJUnit.war"));
// Make sure you use the properties when creating the EJB Container!
        EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);
// Change /classes/ to /WebAppJUnit/
        MyBean instance = (MyBean)container.getContext().lookup("java:global/WebAppJUnit/MyBean");

Et voila!! (Caveat, unless you build the web app the WAR file is not created and Message of Doom appears again).

* Actually I also bind the middleware I develop to a database and to a web UI using PrimeFaces because I am the only web applications developer currently working here.

So now I can, finally, officially perform tests on the software I develop because it is considered essential.

1 comment:

  1. Sir, where should I put those codes in my code line? I didn't get it. Please share it to me... Thank you