14 Juni 2010

EJB 3.1, JUnit and GlassFish Embedded incl. JPA and JDBC DataSource

Today I have my JUnit tests with GlassFish embedded up and running and like to share how to configure it.

First we have a look at how to run the tests with the EJBContainer from EJB 3.1:

public class ConfigurationServiceTest {

    private static EJBContainer container;
    private static ConfigurationService service;

    public static void setUpClass() throws Exception {
        container = EJBContainer.createEJBContainer();
        service = (ConfigurationService) container.getContext().lookup("java:global/classes/ConfigurationService");

    public static void tearDownClass() throws Exception {

     * Test of findConfiguration method, of class ConfigurationService.
    public void testFindConfiguration() throws Exception {
        Configuration configuration = service.findConfiguration();
        System.out.println("From Configuration: " + configuration.getNextNewsletterSendDate());

The most important line is the lookup of the EJB:

Every found EJB is deployed to the embedded server and the JNDI name is built using java:global/classes and then the simple class name of the EJB.

That was all very straight forward. But then I had a problem configuring the JDBC datasource that the service needs using JPA. 
And again the solution was very simple. Just create the path org/glassfish/embed in classpath and add the domain.xml file from your "normal" GlassFish server. After copying you may want to do some cleanup with the file. You can delete everything that is under  <system-applications /> and  <applications /> because your application will run based on the classpath.

That's it.

When you want to us it with Maven2 simply put a new dependency in pom.xml.
Hint: In some situation it is best to have it as the first dependency!


Have fun!


Millimetern hat gesagt…

There are some restrictions that may be good to be aware of. It only works if your domain classes are in the same jar as your ejb:s. Otherwise the container will not load them. It's reported here: https://glassfish.dev.java.net/issues/show_bug.cgi?id=12151. The last comment was on June 12. Not sure whether it made it's way to 3.1, though indicated as target milestone.

Simon Martinelli hat gesagt…

Thanks for your comment.
I don't think that this bug affects my example, because I don't build a jar. In the JUnit test all EJBs found in the classpath are added to the EJBContainer.

for IT the hat gesagt…

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

Oliver Jones hat gesagt…

Very good site you have here but I was wanting to know if you knew of any community forums that cover the same topics discussed in this article? I'd really like to be a part of community where I can get opinions from other experienced individuals that share the same interest. If you have any recommendations, please let me know. Cheers! all of craigslist