Java Reference
In-Depth Information
Let's take a closer look:
B
C
The
setup()
method creates
PlaceOrderServiceImpl
.
The test method calls
makeGoodDeliveryAddress()
and
makeGoodDeliveryTime()
,
which are helper methods that create the test delivery information.
The test method calls the service.
D
E
The test method verifies that the call to the service succeeds and that the delivery
information was stored in the pending order.
Let's look at what needs to be done to get this test to compile and pass.
Implementing the method
After writing a test case, the next step is to write some code to make it compile and
pass. We need to define the
PlaceOrderServiceImpl
class and write an
update-
DeliveryInfo()
method. To write this method, we need to determine which of its
responsibilities it handles directly and which it handles by calling other objects. If
you carefully examine the description of the method given earlier, you will see that
it has four key responsibilities:
Finds or creates the
PendingOrder
1
Verifies that the delivery time is in the future and that the delivery infor-
mation is served by at least one restaurant
2
Updates
PendingOrder
3
Creates
PlaceOrderServiceResult
4
Let's look at each one of these responsibilities in turn, beginning with how
Place-
OrderService
finds or creates
PendingOrder
. Earlier in this chapter I described
how repositories are responsible for creating and finding entities. Consequently,
PlaceOrderService
must call a
PendingOrderRepository
to find or create
Pending-
Order
.
PendingOrderRepository
has a
findOrCreatePendingOrder()
method that
returns
PendingOrder
.
The business logic implemented by this method validates the delivery informa-
tion by checking that the delivery time is in the future and by calling a repository
to verify that there is at least one restaurant in the database that serves the delivery
information.
PlaceOrderService
could be responsible for validating the delivery
information and then updating
PendingOrder
by calling setters. However, this is not
a very robust design since
PendingOrder
should be responsible for ensuring the
Search WWH ::
Custom Search