I am trying to take advantage of Spring's Transactional support in unit tests. I define a transaction manager to rollback after each test using the spring test annotations.
My intention is to create entities in setUp method, execute the test method and rollback the test data after test execution (using rollback).
Spring does good job as far as the DB is concerned. But hibernate search doesn't index the objects as there is no commit on the entities created during setUp (due to defaultRollback parameter).
As the transaction manager doesn't commit the data to the DB, I am not able to test hibernate search queries.
Is there a workaround to setup my tests using Spring's transactional test capabilities to rollback DB/lucene index after each test with out restoring the DB/index through the hard way(tearDown)?
I have the following annotations in my test case where I would like to setup my entities in the DB (setUp method), execute DB queries as part of the same transaction, search the lucene index using hibernate search as part of the same uncommitted transaction (test methods) and let spring handle the rollback of the db and purge the lucene indices as part of rollback.
Code:
RunWith( SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class})
@TransactionConfiguration(transactionManager="transactionManager",
defaultRollback=true)
@Transactional
public class EmployeeTest extends TestCase