Shaun Abram
Technology and Leadership Blog
Running Hibernate unit tests with Maven
I recently converted a project I have been working on to use Maven. After setting up all the dependencies in the pom, I got everything compiling fine but ran into problems getting the unit tests to pick up the hibernate config (hibernate.cfg.xml) and hibernate mapping (*.hbm.xml) files. With hindsight, it is straightforward, but it took me a while to figure it out so I thought I’d post the solution here.
Initially, I had my hibernate.cfg.xml file in the following directory
my-app/src/main/java
And when I first tried to run my hibernate unit tests (mvn test), I got this error:
SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Some checking of the maven docs and some forums revealed the following possible solutions:
1) Copy the hibernate.cfg.xml file to
my-app/target/classes
This works, however I think it is a hack as the target folder is generated rather than being somewhere you should actually manually create files.
2) The next solution I found suggested moving the hibernate.cfg.xml to
my-app/src/main/resources
This works well as the contents of this folder are copied to the base level of the my-app/target/classes folder so it is basically a ‘more correct’ solution than the first.
3) The next and I think best solution was to create a new, duplicate hibernate.cfg.xml inside
my-app/src/test/resources
This allows a different hibernate.cfg.xml file to be used for testing and, for example, facilitates connecting to a different database (such as hsqldb) for testing while continuing to use your regular database (such as MySQL) for the app itself.
A couple of points to note:
1) The hibernate mapping files (i.e. the *.hbm.xml files) should also be in the resources folder (in whatever directory structure you choose) to ensure that these too are accessible by the unit tests.
2) I am not (yet) using Spring with the hibernate components, which would likely have changed the above setup.
Refs/links
http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR
http://www.mail-archive.com/users@maven.apache.org/msg54720.html
Tags: Hibernate, Maven, Testing
Could you give me your pom.xml?
I have the hibernate.cfg.xml in the directory suggestedbut am getting a null pointer exception on
Session session = (Session) entityManager.getDelegate();
This occurrs in a stateless bean.
entityManager is defined as a local variable with
@PersistenceContext(unitName=”Disposer”)
private EntityManager entityManager;
Hi David,
I have emailed my pom to you, but it doesn’t sound like this is a pom or a maven issue. I assume the null pointer exception is being caused by the entityManager being null and if so, is it being injected and/or setup correctly?
Good luck…
Shaun
Thanks Shaun, It worked for me.
Thanks, your solution worked well!
i have *.hbm.xml in config folder .this folder i have added into test-class/config like this ..but still i am getting same error…
java.lang.RuntimeException: org.hibernate.HibernateException: Exception Occured while Building SessionFactory
Pl. Advice.
Anjali,
It’s been a while since I worked on this, but are the contents of test-class/config showing up in your target directory (i.e. where the .class files end up, e.g. /target or /classes)?
Also, are you getting this error when running tests or running your app? Since the mapping files (*.hbm.xml) are usually required by both tests and app, they should be available to both (in my maven example, they should be in the resources folder rather than the test/resources folder).
It would be good to hear if and how you get it working…
Shaun
As an update, Anjali reported that his problem was that he was missing some jar files (he didn’t say which ones unfortunately).
Hi Shaun,
thanks for sharing this information, you saved my day, I was having the problem with it, and I got it fixed because of your post. Very good, I used third solution, its the best.