In unit testing, transactions aren't committed
I've spent the day slowly getting unit testing of my SpringMVC/Hibernate webapp working. The app itself works great. It uses OpenSessionInViewFilter, which I've simulated in my tests with the following:
I'm using an H2 in-memory DB for the testing that I set up once with @BeforeClass. I then have three @Test methods. The first two work as expected, but the third depends on the results of the second being committed to the DB. They do not appear to be committed. The Spring configuration files I'm loading are the exact same ones used by the webapp, so the transaction handling should be the same, assuming my @Before and @After methods are doing the right thing.
// Set up the Hibernate session and transaction…
mSessionFactory = (SessionFactory) mAppContext.getBean("sessionFactory");
Session session = SessionFactoryUtils.getSession(mSessionFactory, true);
TransactionSynchronizationManager.bindResource(mSessionFactory, new SessionHolder(session));
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(mSessionFactory);
My test class is annotated with
The SessionFactory configuration is
@TransactionConfiguration(defaultRollback = false)
which I thought would keep it from rolling back transactions after each test. But I can't seem to get it to commit after each test.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.latencyzero.gamecenter.model"/>
Perhaps I'm trying to do things the wrong way. I intended to make a new test class for each set of tests that should run in a clean DB, but have the individual test methods within a test class depend on each other (and their execution order, which I'm assuming is lexicographical, although I am not sure).
I'm reluctant to put an entire series of tests into one method, because I want the transaction isolation around each particular operation, and I don't think I'll get that implicitly if I put them all into one method.
Moreover, I want the ability to use an embedded H2 DB so I can take browse the data being written. Right now, nothing gets committed.
Any advice would be most appreciated. Thanks!