Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Junit test with Embedded Glassfish stop working with persistence.xml

 
Federico Nafria
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Federico Nafria
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.







 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic