Java Reference
In-Depth Information
mockSqlMapClientTemplate.expects(once())
.method("queryForObject")
.with(eq("findPendingOrder"),
eq("10"))
.will(returnValue(pendingOrder));
C
Sets expectations
PendingOrderDTO result =
dao.findPendingOrder("10");
D
Calls the DAO
Checks return value
E
assertSame(pendingOrder, result);
}
}
Let's take a closer look at this listing:
B
The
setUp()
method creates a mock
SqlMapClientTemplate
, a test
PendingOrder-
DTO
, and a
PendingOrderDAOIBatisImpl
.
The
testFindPendingOrder()
method creates an expectation that
SqlMapClient-
Template.queryForObject()
will be called with a statement name of
findPending-
Order
and a parameter with the value
10
, and then returns the test
PendingOrderDTO
.
The test calls the
findPendingOrder()
method.
The
testFindPendingOrder()
method asserts that the
findOrderCreatePending-
Order()
returns the
PendingOrderDTO
returned by
queryForObject()
.
The test is extremely simple. It requires only a minimal amount of setup and has
no external dependencies. It also runs considerably faster than a test that accesses
the database.
C
D
E
Writing a DAO method
Now that we have written the test, the next step in the process of implementing
the
DAO
is to write the
findPendingOrder()
method. The
PendingOrderDAO-
IBatisImpl
class extends Spring's
SqlMapClientDaoSupport
class and defines a
findPendingOrder()
method, which uses Spring's
SqlMapClientTemplate
to exe-
cute
SQL
statements:
public class PendingOrderDAOIBatisImpl extends
SqlMapClientDaoSupport implements PendingOrderDAO {
public PendingOrderDAOIBatisImpl(
SqlMapClientTemplate template) {
setSqlMapClientTemplate(template);
}
Saves SqlMapClientTemplate
Search WWH ::
Custom Search