Java EE 6 - Embedded Container EJB Tests

This question is about Java EE 6 using Glassfish v3 embedded-all .

I have a unit test that uses an EJBContainer to test my headless EJB. The problem is that I am having problems finding an EJB (remote) using JNDI:

setup() { ctx = EJBContainer.createEJBContainer().getContext(); } ... test() { BookService bookService = (BookService)ctx.lookup("java:global/BookServiceEJB!com.something.service.BookService"); ... } @Stateless public class BookServiceEJB implements BookService { ... } @Remote public interface BookService { ... } 

gives an exception:

 javax.naming.NamingException: Lookup failed for 'java:global/BookServiceEJB!com.something.service.BookService' in SerialContext [Root exception is javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found] ... caused by: javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found 

I tried several JNDI resource paths:

eg.

 java:global/BookServiceEJB java:global/BookService 

even

 java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB 

etc...

nothing works

I do not have xml deployments configured , but only persistence.xml in META-INF.

The test uses maven surefire:

 mvn clean test 

Any help is much appreciated!

Note : full deployment on Glassfish server works (using appclient and @EJB )

+4
java-ee java-ee-6 glassfish-3 jndi
source share
3 answers

After a long search, I found a solution that works for me ...

You need to configure EJBContainer with the property: EJBContainer.MODULES and the location where the module classes are located (if maven is used, 'target / classes').

eg.

 ... props = new Properties(); props.put(EJBContainer.MODULES, new File("target/classes")); ec = EJBContainer.createEJBContainer(props); ... 

If your EJB uses JPA, another problem arises that you cannot determine the data source in the built-in container, so you need to use the standard ds: "jdbc / __ default".

so for example my persistence.xml looks like this:

 <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="bookshelf" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.blah.domain.Book</class> <jta-data-source>jdbc/__default</jta-data-source> <properties> <property name="eclipselink.logging.level" value="INFO"/> </properties> </persistence-unit> </persistence> 

I did not understand how to configure the built-in container test to use one DS (jdbc / __ default), and my application should use another (for example, jdbc / booksDS)

see : http://www.mentby.com/glassfish/embedded-testing-woes.html

see : http://forums.java.net/jive/thread.jspa?messageID=395759

Honestly, I don’t know why people worry about Java EE when solutions like spring are much simpler ...

It was very unpleasant and wasted a lot of time ... hope this helps.

+6
source share

There are several elements that need to be checked to make sure that you can load the bean through context.lookup, avoiding the NamingException.

  • Make sure you have a bean. This may seem like something obvious, but I spent a lot of time trying to understand why I could not get an instance of my service in the tests. The reason was because I was not in the stateless annotation.

  • Add a module when creating the container, as @Ju noted. For maven classes there will be target / classes , for maven tests there will be target / test classes .. p>

  • Something is wrong if you find in the console a message like SEVERE: EJB6005:No EJB modules found . It tells you that there are no annotated classes without state preservation

  • Take a look at the built-in console of fresh fish! There you will see the search names for beans. Pay attention to messages in the INFO: EJB5181:Portable JNDI names for EJB YourBean: [java:global/classes/YourBean!bean.package.YourBean, java:global/classes/YourBean] format INFO: EJB5181:Portable JNDI names for EJB YourBean: [java:global/classes/YourBean!bean.package.YourBean, java:global/classes/YourBean] . This means that you can search for your bean either by calling context.lookup("java:global/classes/YourBean!bean.package.YourBean") or the shorter name context.lookup("java:global/classes/YourBean") , which can be useful if there are no name collisions.

Hope this helps someone. It would be very helpful to have these tips.

+3
source share

I wrote a small tutorial on using the built-in Glassfish 3.1 container, also addressing the issue for having to use another persistence.xml for tests. Also troubleshooting container failures using remote interfaces and web services. You can check it out at http://pschyska.blogspot.com/2011/06/unit-testing-ejb-31-with-netbeans-maven.html

+2
source share

All Articles