I am testing my Hibernate DAOs with Spring and JUnit.
I would like each test method to start with a pre-populated DB, i.e. the Java objects have been saved in the DB, in a Hibernate transaction that has already been committed. How can I do this?
With @After and @Before, methods execute in the same Hibernate transaction as the methods decorated with @Test and @Transactional (first level cache may not be flushed by the time the real test method starts). @BeforeTransaction and @AfterTransaction apparently cannot work with Hibernate because they don’t create transactions even if the method is annotated with @Transactional in addition to @Before/AfterTransaction.
Any suggestion?
One way could be to externalize your intialization logic to an external service with transactional methods which are executed from your @BeforeTransaction and @AfterTransaction annotated methods in the test class.
Another benefit of this approach is the reusability of initialization code across tests.
You could for example use the SpringJunit4ClassRunner like described here like this:
That’s something we do in our Spring based projects.
Make sure the transactional configuration is correct. If you want to avoid the class/interface separation, then set proxy-target-class to true in the element.