Thursday, May 8, 2014

Testing persistence with CDI-Unit and Hibernate.

In the project I' m involved with we 've had multiple tests to evaluate several JPA queries and persistent entities. Our initial approach was to test persistence with Arquillian but time pressure and the size of the development team led us with a daily build lasting about 20 minutes, and we didn't have time to figure out how to refactor our tests to run faster. Furthermore, we were keeping track of our persistent entities in persistence.xml, and due to the size of the test war, it was sufficient to have One New Entity To Break Them All.

Next step was to try with CDI-Unit; to have an EntityManager CDI producer backed up by an H2 database. We were also using EJB3Configuration to programmatically build our EntityManagerFactory. It was brilliant, persistent tests were running as if they were simple unit tests. 

Last week, we 've decided to go with Wildfly; EJB3Configuration wasn't there anymore and we looked for a replacement. After several hours of debugging as we found no documentation for this, we 've managed to make it work :

https://github.com/dmandalidis/cdi-persistence-test.git

To cut a long story short, it worked by overriding :

PersistenceProviderResolver
HibernatePersistenceProvider
EntityManagerFactoryBuilderImpl