Testing EJB3 with Maven2

I am setting up a new JEE project. In this project we will use EJB3 as framework for the business layer. And of course we are using Maven to build, test and deploy the application. One important issue I have to deal with is how to (unit)test this business layer.
I had a look at the following options:

I started with EJB3Unit. I had looked into this one before and based on the name it should do the trick. However, I can’t recall the reason but at that time I decided it didn’t work out for me. This most probably had to do with my limited knowledge of EJB3Unit and is no disqualifier for the product in general!
So the next attempt I took was the one described at this page. This approach was quite easy to understand and I had it running in no time. But after a few days when the project sources were growing I noticed this way of setting up tests needs code duplication and more important: you can only test the JPA part of the simple EJBs. Not something like dependency injection of EJBs into another one and then test that bean. Maybe this isn’t considered as a unit test anymore but it is definitely something I want to test when building my project!
So I looked further and came across OpenEJB. This led to another approach of testing my sources. Instead of setting up two data-sources like in step 2 (one data-source for testing and one for real) I now use 2 EJB containers, each with the same data-source definition, but with actually different database implementations. Although this means I am no longer testing outside the container, it is what I wanted. I can now test all my EJBs, both simple and complex ones. There are a lot of examples about how to use openEJB on their site and it is nicely integrated with Maven2. The only disadvantage that might become a problem is the time a complete compile-test cycling will take if the project become larger. But for now I will go this way.
In a next post I will show the setup of my project to get the whole thing running because there are some small things to be aware of.


About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in EJB3 and tagged , . Bookmark the permalink.