A Techno Blog, mainly about Java

Testing through Spring with rollbacks

If you download the spring framework with dependencies, one of the modules included is the spring-test.

– Convenient jar file combining all standard modules (except for the test module and the Spring MVC support)
– Also includes the AOP Alliance interfaces (as a convenience)!
– Does not include contents of spring-aspects.jar, spring-test.jar and spring-webmvc*.jar!

– Contents: test context framework, JUnit support, JNDI mocks, Servlet API mocks, Portlet API mocks
– Dependencies: spring-core, (spring-context, spring-jdbc, spring-web, JUnit, Servlet API, Portlet API)

As the spring documentation mentions, for unit testing transactional systems you can achieve rollback of your tests pretty easily:

Typically you will extend the subclass, AbstractTransactionalDataSourceSpringContextTests. This class also requires that a DataSource bean definition – again, with any name – be present in the application context. It creates a JdbcTemplate instance variable, that is useful for convenient querying, and provides handy methods to delete the contents of selected tables (remember that the transaction will roll back by default, so this is safe to do).

This problem was identified with MySql.

Nice article here on spring test

This link tells us about the spring testing method calls

AbstractTransactionalDataSourceSpringContextTests superclass
provides the following services:

Injects test dependencies, meaning that we don't need to
perform application context lookups. Injection uses
autowiring by type.

Executes each test method in its own transaction, which is
automatically rolled back by default. This means that even 
if tests insert or otherwise change database state, there
is no need for a teardown or cleanup script.

If you want a transaction to commit--unusual, but useful 
if you want a particular test to populate the database,
for example--you can call the setComplete() method inherited
from AbstractTransactionalSpringContextTests. This will cause
the transaction to commit instead of roll back.

There is also convenient ability to end a transaction before
the test case ends, through calling the endTransaction() method.
This will roll back the transaction by default, and commit it
only if setComplete() had previously been called.

Provides useful inherited protected fields, such as a JdbcTemplate
that can be used to verify database state after test operations,
or verify the results of queries performed by application code.
An ApplicationContext is also inherited, and can be used for
explicit lookup if necessary.

All the Spring Test Life Cycle Methods


  • getConfigLocations
  • setDataSource
  • setTransactionManager
  • onSetUpBeforeTransaction
  • onSetUpInTransaction
  • onTearDownInTransaction
  • endTransaction
  • onTearDownAfterTransaction

example scenario:

Now onto the 2.5 way of testing:

org.springframework.test.context – Spring TestContext Framework which provides annotation-driven unit and integration testing support that is agnostic of the actual testing framework in use.

Spring 2.5 continues this rich and convenient testing framework, while now removing the requirement that your unit tests extend Spring framework tests. Instead of subclassing, it uses java annotations.

Related links:

August 24, 2008 Posted by | IoC, J2EE | 1 Comment