I have an application that uses JPA 2 with Hibernate behind and for unit tests, I use HSQLDB in memory with JUnit 4.11. The HSQLDB is configured in a persistence.xml file in a META-INF folder.
My question is: How do I upload the clean database at the beginning of each test without having to manually call a bunch of "DELETE FROM BLABLA" or something similar?
Currently I have problems within the methods @Before
, a call something like this:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);
The% s of the% s produced are placed in a EntityManager
variable, to ensure that each thread does not manipulate the% s of% s of other threads.
In methods ThreadLocal
I call a EntityManager
.
However, the test is not reliable. Sometimes saved objects disappear for no apparent reason. And often the Hibernate cache fools me by showing objects that are not persisted, but they seem to be, and so I end up abusing the use of @After
as a precaution in places where this should not be necessary.
Does anyone have a better suggestion of some strategy for developing tests using HSQLDB in memory with JUnit?