This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Junit test with Embedded Glassfish stop working with persistence.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Junit test with Embedded Glassfish stop working with persistence.xml" Watch "Junit test with Embedded Glassfish stop working with persistence.xml" New topic
Author

Junit test with Embedded Glassfish stop working with persistence.xml

Federico Nafria
Greenhorn

Joined: Sep 30, 2010
Posts: 9
Hi!

I'm trying to implement testing on Session Beans, and after trying for a few hours, I've been able to make jUnit tests run into Glassfish embedded.

Now Im facing another problem, the tests run fine, until I insert a Persistence.xml inside the META-INF folder. I've "stolen" the persistence.xml from another proyect that uses an installed glassfish (where it works perfectly) and the jdbc resource used in the persistence.xml is defined in the domain.xml file insidesrc/main/resources/org/glassfish/embed

If somebody could tell me why is this happening I'll be extremely thankful!

Driv
Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Hi Frederico,

When you say it stopped working, what is your test result? Is there any exception? Are your entities inside the same project, or do you have separate EJB project and persistence unit project?
I struggled a lot to setup tests with a multi-module maven project and Glassfish embedded, and I used the tips from there: webpage

Bottom line: with GF 3.0.1 embedded, if you have a separate module for the persistence unit, you should copy its content (entities and META-INF/persistence.xml) into the EJB module build directory for the persistence unit to be deployed.

Hope it'll help.

Christian

Christian Gossart
Federico Nafria
Greenhorn

Joined: Sep 30, 2010
Posts: 9
Hi Christian, Thanks for your reply!

I've been able to make it work setting a few properties on the container before starting it


When i didn't do that, the error was the same as if there was no domain.xml in the resources folder. It said that it could not initialize the container.

Another small thing, I'm asking it here, because at this moment I can't try it, is it possible obtain an instance of the EntityManager in a test? I mean using context.lookup(¨xxxx¨) inside the test class, in the same way I obtain the instances of the Session Bean?

Thanks.. ;)
Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Glad you made it work!

tells GF to deploy what's in that directory as a JEE module (your EJBs and your persistence unit) and points GFE to a real install directory.

You can create a small GF domain as described in this blog EJB tests with GFE and have your tests run anywhere without a full GF install.

About the EntityManager, either you create it by hand in your test (it will not be managed by the container) with or you can inject it into an EJB (DAO pattern or not) with @PersistenceContext and manage your data from there.

Christian
Federico Nafria
Greenhorn

Joined: Sep 30, 2010
Posts: 9
Hi Christian, and thanks again!

The blog was extremely clear, I'm going to try it right now. Hope everything works.

I assume that if a want to use another database, I just have to add the datasource and the resource in the domain.xml and put the driver in the lib folder, just like in a full installed glassfish.







 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Junit test with Embedded Glassfish stop working with persistence.xml
 
Similar Threads
Testing jpa part
embedded glassfish 3.1 log files
[Solved] Best practice for testing (Eclipselink) JPA backed web apps?
Testing and JUnit
Eclipse and Glassfish embedded