Original post:
Hi David,
Thanks for your quick reply.
Looking at the first link, the reason why that example works is the implementation of the DAO is specified as a Service component and after a little background reading, Spring applies transactions on service components and that is why that example works.
I will try to verify whether using this annotation solves the problem but it seems weird annotating a DAO impl as a Service instead of a repository. This seems true as using service layer beans annotated @Transactional and @Service, there was no issue in persisting / querying.
I have tried using @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = ...) and using @Transactional on the unit test class + methods but I still ran into the same issue.
I finally did get it to work by having a base test class which initialises the context. Then included a method in the base test class that creates a transaction before each test. The only thing I need to do is handle that open transaction after each test. The only issue is being new to these frameworks, I am not sure whether this is recommended.
Kind regards,
Bennett
Update:
Hi David,
Can you take a look at the org.timesheet.h4 project under:
The two test of interest is org.timesheet.service.dao.EmployeeDaoTestng.
Incase you do not want to download this project and run this test here is what is going on:
- there are 6 tests
- 5 of them test an EmployeeDAO implementation that implements extends an simple supplied HibernateDao & GenericDAOImpl.
- The remainder tests a PersonDAO (an implementation of IPerson) both of which extend and implement this libraries GenericDAO and GenericDAOImpl.
- The 5 tests pass but the PersonDAO test fails with the same reason I have mentioned in this thread.
- I noticed the HibernateDao had @Transactional(propagation= Propagation.REQUIRED, readOnly=false), but placing the same annotation over the PersonDAO had no effect.
Hopefully you can help with this.