Now we just need to add the dependency for the embedded GlassFish server and the Java repository to our pom.xml. It is important to add the dependency for glassfish-embedded-all as the first dependency to the list (-> Troubleshooting)!

So what’s happening here? First we’re starting the embedded EJB Container, lookup the UserEJB via JNDI and assert that the user information we were looking for is present via the EJB.

If you’re not sure what the corresponding JDNI name for your bean is, take a look at the console output on the EJB container startup .. when the UserEJB is registered the following helpful information shows you available JNDI names

Download Sources

I have put the sources for the examples here on Bitbucket .. you may download it there or check it out using

hg clone http://bitbucket.org/hascode/ejb-3.1-embedded-tutorial

Troubleshooting

“java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer“ – I got this error when glassfish-embedded-all was not the first dependency in the dependencies list in my pom.xml – changing the order helped ..

“java.lang.AssertionError: javax.naming.NamingException: Lookup failed for ‘java:global/classes/UserEJB!com.hascode.tutorial.ejb_tutorial.bean.UserEJB’ in SerialContext [Root exception is javax.naming.NameNotFoundException: classes]“ – To fix this error, initialize the EJBContainer instance in the unit test like this (thanks to Ian Smith for mentioning)

This entry was posted
on Saturday, January 1st, 2011 at 3:29 pm and is filed under Java.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Thanks for mentioning your problem One early build of mine failed without the definition of EJBContainer.MODULES creating the embedded container and I found the same solution for this problem on stackoverflow but I’m not sure yet why it failed ..

Another interesting alternative perhaps is the jeeunit project but I haven’t tested it yet

I’ve been struggling lately getting some existing EJB’s to work using embedded GlassFish. It turns out it must be because they implement an interface. I tried to download your “Enterprise Java Bean/EJB 3.1 Testing using Maven and embedded Glassfish” and modify it to let the EJB implement an interface, and even your test fail then.
Do you have any experience testing EJB’s which implement an interface?
I have a project at work where we have used OpenEJB in the past to test the EJB’s, but for the past 2-3 months, the 3.2-SNAPSHOT hasn’t been working.So, I though I should give embedded Glassfish a try, but as I describe, I have a hard time getting them tested because most of them implement an interface! I’ve seen this issue, which I _think_ can be related: http://java.net/jira/browse/GLASSFISH-16547

Perhaps you need to modify the JNDI lookup in the test class … I have modified my tutorial like this: UserEJB implements the interface UserEJBRemote where I just used extract method pattern ..
public class UserEJB implements UserEJBRemote {..}
public interface UserEJBRemote {
public abstract List findAll();
}

Now I changed the ctx.lookup in my test class to this one:
UserEJBRemote userEJB = (UserEJBRemote) ctx.lookup("java:global/classes/UserEJB!com.hascode.tutorial.ejb_tutorial.bean.UserEJBRemote");

Is this a help? Perhaps JBoss Arquillian is also worth a look .. it’s an early version but has some nice features