Tag Archives: ejb3

After working with Spring Framework to build POJO-based services, I found that it is very easy to perform a unit test on them. I just need to build a Service Locator to locate the bean, and Spring will do the rest, by injecting all the dependencies.

Lately, I was learning on how to use EJB3, then it got me thinking. Since, EJB3 session beans can’t live outside their container and they rely on the container for the Dependency-Injection (DI), how on earth do I test them?. I’ve came across a utility, which can help me to test EJB3 components outside their container. It’s an extension of JUnit and it’s cleverly called EJB3Unit. Although, it should be easy to configure but it took me 3 days to figure it out (,after I’ve found out that I had downloaded the incorrect version, that is :p).

First off, you need to download the necessary EJB3Unit jar and its dependencies as shown below.
Required Jars

If you wanted to test your data in your existing persistence layer, you could do that. But as you can see from above diagram, it natively uses Hibernate implementation for the Persistence Provider, and my project is running on GlassFish v2, and by default, it uses Toplink implementation. EJB3Unit also has another great feature which is called In Memory test. It will uses the default database in EJB3Unit, which is by HSQLDB (Hypersonice SQL Database) and auto-generates all the necessary components in the memory. After finishes executing the test methods, it will be cleared off.

Secondly, you need the EJBUnit properties file (ejb3unit.properties) in your classpath. If you need to do an In Memory test, you just have to following below settings:
ejb3unit.properties

Lastly, you need to build the test class such as below:
Session Bean Test

A bit of explanation might be justified. To test a simple session bean, your test class would need to extends from com.bm.testsuite.BaseSessionBeanFixture class and pass your Session bean class as the generic argument (, as shown in the picture, com.dhydrated.business.ProcessBean is my Session Bean). On line 17, you may need to pass your Entities, as array of java.lang.Class, but in my case I want to keep it simple and left it empty. In the constructor, you must call parent’s constructor and pass your Session Bean class and the usedBeans variable. Then the rest will be your test methods. Here, I just have one simple test method called testMyProcessBean. In the method, I’ll get my ProcessBean instance, and the DI will be handled by EJB3Unit. I can just simply call any methods in my ProcessBean instance, and as shown in above diagram, I’m calling my custom method called printMessage and it should returns a message in String object.

Now, I just need to compile and run above class and I should get the below output on my console.

Now, I can rest assured that my Session Bean is working fine before deploying it to the server.

I’ve been reading EJB 3 book for some days now and I’m loving it. For those who has a fair share of hardship on developing with EJB 2.x, would really appreciate the simplicity of EJB 3. Since JEE supports annotation and DI (Dependency Injection), it really helps in making EJB 3 a really good competitor with other POJO oriented services. Prior learning EJB 3, I’m always goes for Spring beans, since it is a better alternative to EJB 2.x. But now, EJB 3 will be one of my top choices. However, Spring framework still is and always be my favorite since it is rich with useful modules. One of my favorite is the security module. I can start building an application with a full-fledged security in a snap and no other can top that.

After 2 weeks without any real work, I was called for a technical interview. It is a norm in my company, where a staff will be interviewed before joining any project. So, I guess, I would excel in being interviewed if I stay in the company long enough. They asked mostly on old technologies such as JSP and JDBC in Java. I just hope I could leverage on EJB 3 in my upcoming projects.