mockcentral: an ejb example
This example use of MockCentral contains a test for an ejb
client that calculates a value based on data obtained from several
Enterprise Java Beans. This value will contain the total number of
"orders" from all "customers" of a specified "customer type" within
"regions" assigned to a particular "manager". The method code being
tested (from the OrderTotaler class) is as
follows:
(note: the Integers managerId and customerTypeId were supplied to the method)
int orderTotal = 0;
// iterate through all regions managed by the indicated manager
Collection regionsUnderManager = regionHome.findByManagerId(managerId);
Iterator regionsIterator = regionsUnderManager.iterator();
while (regionsIterator.hasNext()) {
LocalRegion region = (LocalRegion) regionsIterator.next();
Collection customersInRegion =
customerHome.findByRegionAndType(
(Integer) region.getPrimaryKey(),
customerTypeId);
// iterate through all customers in the region that are of the
// specified type, adding their orders to the total
Iterator customerIterator = customersInRegion.iterator();
while (customerIterator.hasNext()) {
LocalCustomer customer =
(LocalCustomer) customerIterator.next();
// add to the total
orderTotal += customer.getCurrentOrders().intValue();
}
}
return orderTotal;
OrderTotalerTest
class) is as follows:
OrderTotaler totaler = new OrderTotaler();
// assert total orders under the manager with id 1: in our setup,
// this manager oversees the 'northwest' and 'southwest' regions,
// containing 3 and 1 customers of the relevant type, respectively.
int managerId = 1;
int customerTypeId = 1;
assertEquals(
totaler.getOrderTotalByManagerAndCustomerType(
new Integer(managerId),
new Integer(customerTypeId)),
15);
// assert total orders under the manager with id 2: in our setup,
// this manager oversees the 'northeast' and 'southeast' regions,
// containing 2 and 1 customers of type 1, respectively.
managerId = 2;
assertEquals(
totaler.getOrderTotalByManagerAndCustomerType(
new Integer(managerId),
new Integer(customerTypeId)),
11);
// verify method calls on the customerHome Mock Object: expected
// findByRegionAndType() to be called with 4 sets of parameters
server.verifyMock("ejb_example", "customerHome");
// verify method call on the regionHome Mock Object: expected
// findByManagerId() to be called with 1 set of parameters
server.verifyMock("ejb_example", "regionHome");
The full source code is supplied in the src/ejb_example directory of the MockCentral Examples distribution (along with the xml configuration file and an ant build.xml file you can use to run the test and see the output).
discussion
This example illustrates how using MockCentral can eliminate the
need for extensive mock object setup code in your test cases, as
well as the convenience of using the TestingContext
for binding the ejb homes and the simplicity of verifying the mock
object method calls. Our test requires quite a few mock objects:
the ejb home Mocks, four mock objects for Region entities and seven
mock objects for Customer entities. The equivalent java code for
setting up the tests using the MockObjects framework would run to
more than fifty lines. While there are quite a few more
lines in the xml file, this file can be generated rapidly using a
MockCentralEditor application. Of course, having the setup code in
your tests has the advantage that you can see exactly how you've
configured the mock objects while looking at the java file
containing the test itself. However, the logging functionality
built in to MockCentralServer provides this information as well at
runtime in an easy-to-understand format which makes it very simple
to find any problems with your mock object definitions (run the
examples to see the logging output).
