This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Spring, JPA, and Unit/Integration Test

Oct 12th, 2008, 10:22 PM

I am using JPA and my test classes needs access to an EntityManager so it can execute some SQL during setup/teardown.

The issue I have is the test methods are using the same transaction as the actual service methods. I have tried to annotated @Transactional(requires_new) specifically only for the setup methods, but that does not work.

Anyone got a sample on how to inject an entityManager into a test class and have the test class execute certains methods in a transaction context that is different from the transaction context of the service classes?

Have not tried this but it should work. It should be possible to inject a TestDataInjector object into the Test class. You should setup this class in a separate test-context file.

However, this does not "Feel good" to me, your data will be persisted to the database and unless you are using an in-memory database you have to manually remove it. Why not setup in the same transaction?