Naked Objects
Writing tests | |
Modern systems development practice now demands that testing be an integral part of the development process, conducted as early as possible, and automated to the greatest extent possible. In the Java world, the Junit framework has been one of the most significant enablers of this concept. The following code shows one fairly obvious approach to writing unit tests for Naked Objects using Junit. This tests the bidirectional association between a booking and the customer, and the confirm option: public void testBookingForCustomer() { Booking booking = new Booking(); Customer customer = new Customer(); booking.associateCustomer(customer); assertEquals(customer, booking.getCustomer()); assertTrue(customer.getBookings().contains(booking)); assertTrue(booking.aboutActionCheckAvailability().canUse().isAllowed()); booking.actionCheckAvailability(); assertEquals("Available", booking.getStatus().title().toString()); } The code below re-writes this using 'mock' view objects, which are provided as part of the Naked Objects framework. Mock views represent the objects within test code in the same way that graphical views represent the objects within the basic GUI, i.e. they allow the objects to be manipulated as if a user were working interactively. As above, we start by creating two new objects, a booking and a customer. This time, though, we create the new booking by getting a mock view of the booking class itself (using the name 'Bookings' that would normally be displayed under the class icon), and then performing the equivalent of right-clicking on the menu for that class and invoking the New Instance... option. We do the same for the customer: public void testBookingForCustomer() { View booking = getClassView("Bookings").newInstance(); View customer = getClassView("Customers").newInstance(); booking.drop("Customer", customer.drag()); booking.assertFieldContains("Customer", customer); customer.assertFieldContains("Bookings", booking); booking.rightClick("Check Availability"); booking.assertFieldContains("Status", "Available"); } The advantage of this approach over the previous Junit example is
not only that the test code is simpler, but that the mock views test the
functionality from the perspective of the user. Thus we no longer have
to worry about whether to call the The example also shows some additional
|
|
Copyright (c) 2002 nakedobjects.org You may print this document for your own personal use, or you may copy it in electronic form for access within your organisation, provided this notice is preserved. |