Java Reference
In-Depth Information
knowledge about the structure of the mapping document but does require the
ORM
framework to expose the necessary
API
s. I describe how to write
ORM
tests in
more detail in chapters 5 and 6.
Mock object testing of repositories
We could test a repository using database-level tests. For example, one way to test
a repository method that executes a query is to populate data with test objects, call
the method, and verify that it returns the expected objects. The problem with this
approach is that it tests several things simultaneously: the repository, any queries
that it executes, and the
O/R
mapping. The test needs a lot of setup and executes
slowly. A better approach, which reduces the number of test cases and database
accesses, is to test the repository using mock objects and to test the queries against
the database separately.
Consider, for example, the
PendingOrder.createPendingOrder()
method, which
creates a
PendingOrder
in the database. One way to test this method is to write a test
that calls it and then verifies that it inserted a row into the
PENDING_ORDER
table.
However, if you have written tests for the object/relational mapping, then you can
safely assume that
HibernateTemplate.save()
or
JdoTemplate.makePersistent()
will work as expected. The repository test does not need to verify that a
PendingOrder
will be inserted into the
PENDING_ORDER
table when the repository calls
save()
or
makePersistent()
. We can therefore simplify and speed up the repository test by
using a mock object for
HibernateTemplate
or
JdoTemplate
and verifying that the
repository calls
save()
or
makePersistent()
as expected. We can use a similar
approach to test other repository methods.
4.5.4
Overview of ORMUnit
In order to make it easier to write tests for the
O/R
mapping and persistent
objects, I've written a simple
JU
nit extension called
ORMU
nit. It provides several
base classes that extend
JUnitTestCase
:
■
HibernateMappingTests
: For testing a Hibernate object/relational mapping
■
JDOMappingTests
: For testing a
JDO
object/relational mapping
■
HibernatePersistenceTests
: For testing Hibernate objects and queries
JDOPersistenceTests
: For testing
JDO
objects and queries
■
HibernateMappingTests
and
JDOMappingTests
simplify the task of testing the object/
relational mapping. They provide methods for making assertions about the map-
ping and for verifying that it matches the database schema. For example, they make
it easy to write a test that verifies that all of a class's fields are mapped to the database.
Search WWH ::
Custom Search