Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

In unit testing, transactions aren't committed

$
0
0
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:

Code:

@Before
public
void
setup()
{
    sLogger.debug("setup");
   
    //  Set up the Hibernate session and transaction…
   
    mSessionFactory = (SessionFactory) mAppContext.getBean("sessionFactory");
    Session session = SessionFactoryUtils.getSession(mSessionFactory, true);
    TransactionSynchronizationManager.bindResource(mSessionFactory, new SessionHolder(session));
}

@After
public
void
tearDown()
{
    sLogger.debug("teardown");
   
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(mSessionFactory);
    //SessionFactoryUtils.releaseSession(sessionHolder.getSession(), mSessionFactory);
    SessionFactoryUtils.closeSession(sessionHolder.getSession());
}

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.

My test class is annotated with

Code:

@TransactionConfiguration(defaultRollback = false)
The SessionFactory configuration is

Code:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.latencyzero.gamecenter.model"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">false</prop>
            <prop key="hibernate.generate_statistics">true</prop>
        </props>
    </property>
</bean>

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.

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!

Viewing all articles
Browse latest Browse all 297

Trending Articles