Framework for Tests of Persistence
When we work with persistent data, at some point, no matter how good our unit tests are,
we must execute integration tests against a database.
We need to ensure that the object-relational mapping is implemented
correctly, that CRUD operations work, that finder methods return the
right values.
We want to be able to run and re-run these tests at any time,
so the tests need to be stateless.
Of course, a database necessarily introduces state, so steps must be
taken to return the database to its previous state after completing
each test.
Similarly, execution of some tests requires that the database be
put in some initial state, perhaps pre-populated with certain values
before the main body of the test executes.
Let's see how the Case Study uses the SpringUnit framework to enable this behavior.
The Spring framework class
AbstractTransactionalSpringContextTests
handles the problem of stateless database testing by enclosing test
operations inside a transaction, and by default, rolling back that
transaction at the conclusion of the test.
To see where this is accomplished, examine the
onSetUp
method,
where
startNewTransaction
is called,
and correspondingly,
onTearDown
, which calls
endTransaction
.
The following diagram shows the relationships between classes from the
Spring framework, the SpringUnit framework, and the Case Study.