Transactional Test Logic
The logic for the test of
insertOrder
is straightforward.
The test retrieves an
Order
,
a list of
LineItem
s, and
an expected total price from the SpringUnit context.
It constructs the
Order
by adding all of the
LineItem
s to the order,
and then calls the
PetStoreImpl
to insert the order.
Next, the test reads back the order from the database
and performs a sanity check for correctness by comparing
the total price of the actual
Order
with
the expected total price.
PetStoreImplTransactionTest.java (excerpt)
public class PetStoreImplTransactionTest extends SpringUnitTest {
public void testInsertOrder() throws Exception {
Order order = getObject("order");
List<? extends LineItem> lineItems = (List<? extends LineItem>)getObject("lineItems");
for (LineItem lineItem : lineItems) {
order.addLineItem(lineItem);
}
getPetStoreService().insertOrder(order);
Order actual = getPetStoreService().getOrder(order.getId());
double expectedTotalPrice = (Double)getObject("expectedTotalPrice");
double actualTotalPrice = actual.getTotalPrice();
assertEquals(expectedTotalPrice, actualTotalPrice);
}
/* Other methods and fields omitted */
}
Recall that
LineItem
s depend upon
Item
, and transitively, upon
Product
and
Category
as well.
Those dependencies must be created before the
insertOrder
method is called.
Also note that both the call to
insertOrder
and
getOrder
are transactional; in particular,
the former changed the state of the database.
We will see next how each of these issues is addressed.