Java Reference
In-Depth Information
PlaceOrderServiceResult result =
service.updateDeliveryInfo(
pendingOrderId,
goodDeliveryAddress,
goodDeliveryTime);
E
Invokes the
service
assertTrue(result.isSuccess());
F
Verifies
the result
PendingOrder returnedPendingOrder =
result.getPendingOrder();
assertSame(pendingOrder,
returnedPendingOrder);
}
}
Let's look at what this test does:
B
The test case class extends the jMock
MockObjectTestCase
class, which automati-
cally verifies that the mock object's expectations are met.
C
The
setup()
method creates the mock
PendingOrderRepository
, the mock
Pend-
ingOrder
, and
PlaceOrderService
.
The test defines the expectations for the mocks and their return values. The mock
PendingOrderRepository
expects to have
findOrCreatePendingOrder()
called with
the delivery information and returns the mock
PendingOrder
. The mock
Pending-
Order
expects to have
updateDeliveryInfo()
called with the delivery information.
It returns
true
to indicate that the delivery information was valid and that
Pending-
Order
was updated.
After configuring the expectations, the test calls
PlaceOrderService.update-
DeliveryInfo()
.
The test then asserts that the call succeeds and verifies that
PlaceOrderService
returned the mock
PendingOrder
. It no longer verifies that
PendingOrder
contains
the correct delivery information because it assumes that
PendingOrder.update-
DeliveryInfo()
behaves correctly.
As you can see, this method and its tests are relatively simple because like most
service methods it simply invokes other domain model objects. By using mocks for
those objects, we can develop and test
PlaceOrderService
without having to get
into their implementation details.
D
E
F
Search WWH ::
Custom Search