Java Reference
In-Depth Information
Similarly, the delivery information parameters can be valid or invalid for a variety
of reasons. In order to flesh out and thoroughly test the transaction script, we
must write tests for several combinations of arguments. Let's look a test for the
scenario where
updateDeliveryInfo()
is invoked with a
null
pending order
ID
and valid delivery information.
In this scenario,
updateDeliveryInfo()
fulfills its responsibilities as follows:
Creates the
PendingOrder
—It can call
PendingOrderDAO.createPending-
Order()
.
1
Verifies that the delivery time in the future
—It can do this with some simple
conditional logic.
2
Finds the available restaurants
—It can retrieve the available restaurants by
calling the
RestaurantDAO
, which defines a
findAvailableRestaurants()
method. This method returns the list of restaurants that serve the speci-
fied delivery information.
3
Stores the delivery information in the
PendingOrder
—
updateDeliveryInfo()
can
do this by simply calling setters on the
PendingOrderDTO
. Remember that
unlike the
PendingOrder
domain object, the
PendingOrderDTO
does not
implement any business logic and so doesn't validate the data that it contains.
4
Saves it in the database
—It saves the
PendingOrder
in the database by calling
PendingOrderDAO.savePendingOrder()
, which updates the
PENDING_OR-
DER
table.
5
Returns an
UpdateDeliveryInfoResult
that contains the updated
Pending-
Order
and the available restaurants
—It creates the
UpdateDeliveryInfo-
Result
by calling
new
.
6
We can write a test that verifies that
updateDeliveryInfo()
does this by using
mock objects for the
PendingOrderDAO
,
RestaurantDAO
, and
PendingOrderDTO
. The
test configures these mock objects to expect particular methods to be called and
return test values, and passes the mock
DAO
s to
PlaceOrderTransaction-
ScriptsImpl
as constructor arguments. It then calls the transaction script and ver-
ifies that it returns the expected
DTO
. Listing 9.1 shows the test method.
Listing 9.1
PlaceOrderTransactionScriptsImplTests
public class PlaceOrderTransactionScriptsImplTests
extends MockObjectTestCase {
private Mock mockPendingOrderDAO;
Search WWH ::
Custom Search